Phonegap FileTransfer错误代码返回null [英] Phonegap FileTransfer error code returns null

查看:161
本文介绍了Phonegap FileTransfer错误代码返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been implementing upload picture functionality in Phonegap. My server side code is already ready.

Ive安装了plugins File和FileTransfer,但是当我尝试将图片上传到我的服务器时,代码总是失败,将总是去fail()函数。我不能确定什么是错误,因为error.code,error.source,error.target总是null。

Ive installed plugins File and FileTransfer but when i try to upload a picture to my server, the code always fail and will always go to fail() function. I cannot determine whats the error since error.code, error.source, error.target is always null.

为什么我在error.code上获得null?我不能确定这里的错误,所以我真的需要你的帮助。

Why am i getting null on error.code? I cannot determine whats wrong here so i really need your help.

这是我的代码:

           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();


推荐答案

在config.xml中修复它

Fixed it in 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>

Im使用phonegap 3.4.0 for Android

Im using phonegap 3.4.0 for Android

这篇关于Phonegap FileTransfer错误代码返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆