Reputation: 5632
I answered this question myself since it took me a long time to find the solution for it and it wasn't documented very well.
Upvotes: 8
Views: 13635
Reputation: 17382
Error code 3 is a rather broad error; it basically means your server isn't coded correctly or you don't have an internet connection, and that results in a connection error.
Could mean:
upload_max_filesize
setting in php.ini, to fix this on ExpressJS you need to adjust the limit
field for Multer, etc. Basically, increase the file upload size on the server. Most servers limit file upload sizes as a security measure. (https://www.owasp.org/index.php/Unrestricted_File_Upload)options.fileKey
value (i.e. <input type="file" name="fileKey" />
) isn't the name your server expects - an example error message might be "unexpected field".content-type
field in the header does not have a value of multipart/form-data; boundary=----WebKitFormBoundary
. Logging the request header on the server, could be used to check if content-type is correctly set.@AugieGardner - Also in agreement that the Cordova File Transfer plugin isn't well documented for uploading photos taken with the Camera plugin.
Fortunately, I have a working example for iOS (and my guess is Android as well):
cordova file transfer plugin not working in ios simulator
A simpler alternative (or fallback), would be to encode the image as Base64, and send it through a plain old AJAX POST request. Which includes the following advantages and disadvantages.
Disadvantages of Base64 encoded images sent over AJAX
Advantages of Base64 encoded images sent over AJAX
Upvotes: 0
Reputation: 5632
While trying to use FileTransfer() to upload images from a phonegap app on android to a remote server i kept getting an error code 3 on every alternate file upload.
It worked once but instantly when i tried again it would throw an error without even sending the file to the server.
The code i am using for the file upload was :
The key to making it work was to add a header option.
options.headers = {
Connection: "close"
}
options.chunkedMode = false;
The complete code :
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType="image/jpeg";
options.chunkedMode = false;
*options.headers = {
Connection: "close"
};*
// setup parameters
var params = {};
params.fullpath =imageURI;
params.name = options.fileName;
var ft = new FileTransfer();
ft.upload(imageURI, encodeURI(url+'/account/profile-pics'), win, fail, options);
function win(r) {
//file uploaded successfully
}
function fail(error) {
alert("An error has occurred: Code = " + error.code);
alert("upload error source " + error.source);
alert("upload error target " + error.target);
}
Upvotes: 22