【问题标题】:Phonegap FileTransfer error code returns nullPhonegap FileTransfer 错误代码返回 null
【发布时间】:2014-05-07 06:55:24
【问题描述】:

我一直在 Phonegap 中实现上传图片功能。 我的服务器端代码已经准备好了。

我已经安装了插件 File 和 FileTransfer,但是当我尝试将图片上传到我的服务器时,代码总是失败并且总是会转到 fail() 函数。我无法确定错误是什么,因为 error.code、error.source、error.target 始终为 null。

为什么我在 error.code 上得到空值?我无法确定这里出了什么问题,所以我真的需要你的帮助。

这是我的代码:

           var pictureSource = navigator.camera.PictureSourceType;
           var destinationType = navigator.camera.DestinationType;

                    function clearCache() {
                        navigator.camera.cleanup();
                    }

                    var retries = 0;
                    function onCapturePhoto(fileURI) {
                        var win = function (r) {
                            clearCache();
                            retries = 0;
                            alert('Done!');
                            console.log(r);
                        }

                        var fail = function (error) {
                            alert("An error has occurred: Code = " + error.code);
                            console.log("upload error source " + error.source);
                            console.log("upload error target " + error.target);
                            if (retries == 0) {
                                retries ++
                                setTimeout(function() {
                                    onCapturePhoto(fileURI)
                                }, 1000)
                            } else {
                                retries = 0;
                                clearCache();
                                alert('Ups. Something wrong happened!');
                            }
                        }

                        var options = new FileUploadOptions();
                        options.fileKey = "profilepic";
                        options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);
                        options.mimeType = "image/jpeg";
                        options.chunkedMode = false;
                        options.params = {atoken : app.atoken, user_id : window.localStorage.getItem("user_id")}; // if we need to send parameters to the server request
                        var ft = new FileTransfer();
                        //alert(app.base_url + "/apiuser/updatePic");
                        ft.upload(fileURI, encodeURI(app.base_url + "/apiuser/updatePic"), win, fail, options);
                    }

                    function capturePhoto() {
                        navigator.camera.getPicture(onCapturePhoto, onFail, {
                            quality: 100,
                            sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY,
                            destinationType: destinationType.FILE_URI
                        });
                    }

                    function onFail(message) {
                        alert('Failed because: ' + message);
                    }

                    capturePhoto();

【问题讨论】:

    标签: cordova phonegap-plugins file-transfer cordova-plugins error-code


    【解决方案1】:

    在 config.xml 中修复它

    我用过

    <gap:plugin name="org.apache.cordova.file"/>
    <gap:plugin name="org.apache.cordova.file-transfer"/>
    

    而不是

    <feature name="File">
       <param name="android-package" value="org.apache.cordova.FileUtils" />
    </feature>
    <feature name="FileTransfer">
       <param name="android-package" value="org.apache.cordova.FileTransfer" />
    </feature>
    

    我正在使用适用于 Android 的 phonegap 3.4.0

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-24
      • 1970-01-01
      • 2014-01-09
      • 2015-03-23
      相关资源
      最近更新 更多