简体   繁体   中英

Cordova / Phonegap FileTransfer iOS Error 1

I'm coding a Cordova app and it uploads image that have either been taken from the camera or from the gallery to a AWS server.

I've set it up so it works perfect in android, but when I run the same code in iOS it doesn't run as intended.

The filetransfer upload returns an error 1, which according to the docs indicates the file to upload hasn't been found. However, the file URI I'm trying to transfer seems to be correct as I'm able to open that URI in Safari and it locates the image.

Here is the error:

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/";}

And here is the code in question:

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

I should note that I've clarified that the socket is returning the correct variables for the AWS authentication.

OK So fixed it myself. Basically the code is mostly fine as above, but the input URI contained spaces which fileupload can't handle.

So I added the following code at the top:

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

Solved the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM