Phonegap File Transferring a failure image on every other image: Error code 3 with FileTransfer loading

I myself answered this question, since it took me a long time to find a solution for it, and it was not documented very well.

+8
cordova phonegap-plugins
source share
2 answers

When I tried to use FileTransfer () to upload images from phonegap to an Android server on a remote server, I continued to receive error code 3 for each alternative 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 use to download the file was:

The key to his work was to add a header parameter .

options.headers = { Connection: "close" } options.chunkedMode = false; 


Full 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); } 
+22
source share

Error Code 3 is a fairly wide error; this basically means that your server is incorrectly encoded or you do not have an Internet connection, and this leads to a connection error.

Can mean:

  • You do not have a multi-board plugin installed on the server. In PHP set "file_uploads = On" (in PHP.ini), in ExpressJS you need the Multer middleware plugin ( https://www.npmjs.com/package/multer ), etc.
  • The fact that downloading the file is larger than your server allows - which leads to an error of the status code 413, with the message "request object is too large". To fix this in PHP, you need to configure the upload_max_filesize parameter in php.ini, to fix it in ExpressJS, you need to configure the limit field for Multer, etc. Basically, increase the file upload size on the server. Most servers limit file upload size as a security measure. ( https://www.owasp.org/index.php/Unrestricted_File_Upload )
  • That the value of options.fileKey (i.e. <input type="file" name="fileKey" /> ) is not the name your server expects - an example error message might be an "unexpected field".
  • That the content-type field in the header does not matter multipart/form-data; boundary=----WebKitFormBoundary multipart/form-data; boundary=----WebKitFormBoundary . Registering the request header on the server can be used to verify that the content type is set correctly.

Upload photos with file transfer

@AugieGardner is also in agreement that the Cordova file transfer plugin is not well documented to upload photos taken with the camera plugin.

Fortunately, I have a working example for iOS (and I think Android too):
cordova file transfer plugin not working in ios simulator

Upload photos without transferring files

The simplest alternative (or fallback) would be to encode the image as Base64 and send it through a plain old AJAX POST request. This includes the following advantages and disadvantages.

Disadvantages of Base64 encoded images sent via AJAX

  • You may need to increase the size of the limit on your server, so you won’t get 413 errors (i.e., "request object too large").
  • Base64 images are about 37% larger than binary images, which most likely results in slower downloads.
  • May not be suitable for videos or other file types.

Benefits of Base64 encoded images sent via AJAX

  • Smaller application size (and faster application loading) because the file transfer plugin in Cordoba and, possibly, the Cordova File plugin will not add application overhead.
  • You do not need to fix the errors of the File Transfer plugin when scaling the application to new operating systems (for example, iOS, Android, etc.).
  • You may not need a multi-page service (or middleware) on the server to download images.
0
source share

All Articles