【问题标题】:How to get image name after upload to server in cordova如何在科尔多瓦上传到服务器后获取图像名称
【发布时间】:2016-06-07 18:37:08
【问题描述】:

大家好,我正在使用 jquery、ajax 和 html。这是我上传的照片

    function uploadPhoto(imageURI) {
         if (imageURI.substring(0,21)=="content://com.android") {
          photo_split=imageURI.split("%3A");
          imageURI="content://media/external/images/media/"+photo_split[1];
        }
        var options = new FileUploadOptions();
        options.fileKey="file";
        options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
        //alert(options.fileName);
        options.mimeType="image/jpeg";

        var params = new Object();
        params.value1 = "test";
        params.value2 = "param";

        options.params = params;
        options.chunkedMode = false;

        var ft = new FileTransfer();
        ft.upload(imageURI, host+"/skripsitemplate/php/upload.php", win, fail, options);
    }

    function win(r) {
        console.log("Code = " + r.responseCode);
        console.log("Response = " + r.response);
        console.log("Sent = " + r.bytesSent);
    }

此代码工作正常。但我不知道如何获取已上传到服务器的图像名称。图像名称将被上传到数据库。这是我的upload.php

<?php
print_r($_FILES);
$new_image_name = "foto".rand(1,1000)."_".date("Y-m-d-h-i-s").".jpg";
move_uploaded_file($_FILES["file"]["tmp_name"], "../web/uploads/".$new_image_name);
echo $new_image_name;
?>

我真的是关于cordova和phonegap的新手。谢谢你们,祝你们有美好的一天

【问题讨论】:

    标签: jquery cordova


    【解决方案1】:

    根据您的PHP代码,上传的图片文件名在echo $new_image_name;中作为响应返回,您可以在win函数中作为对象响应访问它:

     function win(r) {
        console.log("Code = " + r.responseCode);
        console.log("Response = " + r.response); 
        console.log("Sent = " + r.bytesSent);
        alert("filename is" + r.response); // <-- this is your filename
    }
    

    但是,您需要删除 $_FILES 对象上的 print_r 函数,因为它会提供不需要的额外输出。

    <?php
    // print_r($_FILES); // <---- remove this 
    $new_image_name = "foto".rand(1,1000)."_".date("Y-m-d-h-i-s").".jpg";
    move_uploaded_file($_FILES["file"]["tmp_name"], "../web/uploads/".$new_image_name);
    echo $new_image_name;
    ?>
    

    【讨论】:

    • 非常感谢@Shadi Shaaban
    猜你喜欢
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 2016-04-09
    • 2015-01-25
    • 1970-01-01
    • 2016-03-11
    相关资源
    最近更新 更多