Cordova / Phonegap FileTransfer iOS错误1

时间:2014-06-03 21:18:30

标签: ios angularjs cordova amazon-web-services file-transfer

我正在编写Cordova应用程序,并将上传的图像从相机或图库上传到AWS服务器。

我已将其设置为在Android中运行完美,但是当我在iOS中运行相同的代码时,它并没有按预期运行。

文件传输上传返回错误1,根据文档指示尚未找到要上传的文件。但是,尝试传输的文件URI似乎是正确的,因为我能够在Safari中打开该URI并找到图像。

这是错误:

FileTransferError {
code = 1;
source = "file:///Users/michael/Library/Application Support/iPhone Simulator/7.1-64/Applications/81CAB36F-9B14-42D3-8A00-06FE37A415EB/Documents/.thumbs/albumCover_b9f71c26291844c7f96a9a43fc4c61b6.jpg";
target = "http://***.s3.amazonaws.com/";}

这是有问题的代码:

var url = "http://***.s3.amazonaws.com/";
            var uploadSuccess = function(r) {
                console.log("Full Image Uploaded");
            }
            var uploadError = function(error) {
                console.log("Upload Failed, Error: ", error.code);
            };
            var fullUpload = function() {
                socket.emit('getAWSCredentials', {pictureID: imageURI.substr(imageURI.lastIndexOf('/') + 1)}, function(policy) {
                    var options = new FileUploadOptions();
                    options.fileKey = "file";
                    options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
                    options.mimeType = "image/jpeg";

                    var params = {
                        AWSAccessKeyId: policy.awsKey,
                        policy: policy.policy,
                        signature: policy.signature,
                        key: policy.key,
                        acl: "public-read",
                        "Content-Type": "image/jpeg"
                    };
                    options.params = params;
                    options.chunkedMode = false;

                    var ft = new FileTransfer();
                    ft.upload(imageURI, encodeURI(url), uploadSuccess, uploadError, options);
                });
            }   
fullUpload();

我应该注意到,我已经澄清了套接字正在为AWS身份验证返回正确的变量。

1 个答案:

答案 0 :(得分:1)

好的,我自己修好了。基本上代码大致如上所述,但输入URI包含fileupload无法处理的空格。

所以我在顶部添加了以下代码:

imageURI=imageURI.replace(/ /g,"%20");

解决了这个问题。