【问题标题】:How to upload file with Phonegap and JqueryMobile?如何使用 Phonegap 和 JqueryMobile 上传文件?
【发布时间】:2012-12-13 13:53:56
【问题描述】:

我正在使用 JQM 和 PhoneGap 构建适用于 Android 的移动应用程序。 我需要将文件(图像)上传到远程服务器(从画廊或用相机拍照)。 基本上可以使用phonegap文件AP​​I来完成,问题是服务器被写成支持简单的POST提交。

我需要在我的应用请求中“模拟”exact,因为它会从以下 html 表单发送。 另外我需要得到服务器的响应。

<form name="myWebForm" ENCTYPE="multipart/form-data" action="http://www.myurl.com/api/uploadImage "method="post">
    <input type="file" name="image" />
    <input type="submit" value="Submit"/>       
</form>

我尝试使用 phonegap 文件 API,但在服务器端检索到的数据的结构与应有的不同。

我尝试在我的应用中实现该表单,但“选择文件”按钮被禁用...

如何在服务器端不做任何改动的情况下实现?

【问题讨论】:

标签: jquery cordova jquery-mobile


【解决方案1】:

您不能在 Phonegap 上使用输入文件。不支持。你需要做这样的事情:

    function onDeviceReady() {

        // Retrieve image file location from specified source
        navigator.camera.getPicture(uploadPhoto,
                                    function(message) { alert('get picture failed'); },
                                    { quality: 50, 
                                    destinationType: navigator.camera.DestinationType.FILE_URI,
                                    sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY }
                                    );

    }

    function uploadPhoto(imageURI) {
        var options = new FileUploadOptions();
        options.fileKey="file";
        options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1)+'.png';
        options.mimeType="text/plain";

        var params = new Object();

        options.params = params;

        var ft = new FileTransfer();
        ft.upload(imageURI, encodeURI("http://some.server.com/upload.php"), win, fail, options);
    }

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

    function fail(error) {
        alert("An error has occurred: Code = " + error.code);
        console.log("upload error source " + error.source);
        console.log("upload error target " + error.target);
    }

在 getPicture 方法中,您将选择文件源。查看更多信息:http://docs.phonegap.com/en/2.1.0/cordova_file_file.md.html#FileTransfer

编辑:

需要指定文件扩展名以及以“文本/纯文本”格式请求的 mimeType 以发送文本格式的图像。 至于参数,如果你不需要它们,为什么要使用它们?

【讨论】:

  • 这正是我使用的代码,但是这段代码在服务器端的结构不像是通过表单提交的。该方法传递一个名为“files”的数组,该数组有另一个名为“file”的数组(options.fileKey="file";),并且“file”数组具有名称、类型、大小等属性......问题在于服务器端正在寻找保存图像的名为“图像”的变量......就像在表单中......还有其他建议吗?
  • 查看将 mimeType 更改为 text/plain 并发送空参数。
  • 仍然不起作用...是否可以通过编程方式从 navigator.camera.getPicture 提交返回的对象而无需 phonegap?仅使用 jquery 或 javascript?
  • 这是我找到上传的唯一解决方案。我忘了告诉您需要将文件扩展名放在文件名上,例如 options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1)+".png";
  • 对不起。但我对你没有更多的选择。
【解决方案2】:

您不能在 Phonegap 上使用输入文件。不支持。你需要做这样的事情:

        <div ng-click="selectPicture()">selectPicture</div> // Put own HTML format but call the fuction

        // Angular fuction
             $scope.selectPicture = function () {
                var srcType = Camera.PictureSourceType.SAVEDPHOTOALBUM;
                var options = {
                    // Some common settings are 20, 50, and 100
                    quality: 50,
                    destinationType: Camera.DestinationType.FILE_URI,
                    // In this app, dynamically set the picture source, Camera or photo gallery
                    sourceType: srcType,
                    encodingType: Camera.EncodingType.JPEG,
                    mediaType: Camera.MediaType.PICTURE,
                    allowEdit: true,
                    correctOrientation: true  //Corrects Android orientation quirks
                }
                navigator.camera.getPicture(function cameraSuccess(imageUri) {
                    MobileUploadFile(imageUri);

                }, function cameraError(error) {
                    console.debug("Unable to obtain picture: " + error, "app");
                }, options);
            }

            function MobileUploadFile(imageURI) {
                //console.log(imageURI);   
                var options = new FileUploadOptions();
                options.fileKey = "file";
                options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
                options.mimeType="image/jpeg";
                options.chunkedMode = false;

                var ft = new FileTransfer();
                ft.upload(imageURI, encodeURI("http://www.example.com/upload.php"), function(result){
                    //console.log(JSON.stringify(result));
                }, function(error){
                    //console.log(JSON.stringify(error));
                }, options);
            };

     // php file
     <?php 
       // Directory where uploaded images are saved
        $dirname = "uploads/"; 
        // If uploading file
         if ($_FILES) {
             print_r($_FILES);
             mkdir ($dirname, 0777, true);
             move_uploaded_file($_FILES["file"]{"tmp_name"],$dirname."/".$_FILES["file"]["name"]);
        }
      ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    相关资源
    最近更新 更多