Skip to content Skip to sidebar Skip to footer

Cordova/phonegap: Filetransfererror.file_not_found_err With File Transfer Plugin

In my phonegap app I take a picture with my camera and it works as expected. Then, I'd like to send it to my server. I see that sending the base64 encoded string is a bad practice

Solution 1:

You need to take a picture and get a FileEntry Object

If you need to use a FileEntry object, set the destinationType to Camera.DestinationType.FILE_URI in your CameraOptions object

UPDATED CODE

functiongetPicture(){

      var srcType = Camera.PictureSourceType.CAMERA;
      var options = setOptions(srcType);
      //var func = createNewFileEntry;

      navigator.camera.getPicture(functioncameraSuccess(imageUri) {

          //displayImage(imageUri);// You may choose to copy the picture, save it somewhere, or upload.//func(imageUri);var filename = imageUri.substr(imageUri.lastIndexOf('/') + 1);
          window.resolveLocalFileSystemURL(cordova.file.cacheDirectory, functionsuccess(dirEntry) {

              // Do something with the FileEntry object, like write to it, upload it, etc.// writeFile(fileEntry, imgUri);console.log("got file: " + dirEntry.fullPath);

              console.log("file name " + filename);
              // JPEG file
              dirEntry.getFile(filename, { create: true, exclusive: false }, function (fileEntry) {

                      // Do something with it, like write to it, upload it, etc.// writeFile(fileEntry, imgUri);console.log("got file: " + fileEntry.fullPath);
                      // displayFileData(fileEntry.fullPath, "File copied to");upload(fileEntry);

                  }, onErrorCreateFile);                      
              // displayFileData(fileEntry.nativeURL, "Native URL");
          }, function () {
            // If don't get the FileEntry (which may happen when testing// on some emulators), copy to a new FileEntry.//createNewFileEntry(imgUri);
          });
          //console.log(imageUri);

      }, functioncameraError(error) {
          console.debug("Unable to obtain picture: " + error, "app");

      }, options);
 };

set option function.

functionsetOptions(srcType) {
  var options = {
      // Some common settings are 20, 50, and 100quality: 50,
      destinationType: Camera.DestinationType.NATIVE_URI ,
      // In this app, dynamically set the picture source, Camera or photo gallerysourceType: srcType,
      encodingType: Camera.EncodingType.JPEG,
      mediaType: Camera.MediaType.PICTURE,
      allowEdit: false,
      saveToPhotoAlbum:false,
      correctOrientation: true//Corrects Android orientation quirks
  };
  return options;
}

See the detail explanation here.

To Upload the file to the server

functionupload(fileEntry) {

 var fileURL = fileEntry.toURL();

 var success = function (r) {
     console.log("Successful upload..." + JSON.stringify(r));
     console.log("Code = " + JSON.stringify(r.responseCode));
     alert(JSON.stringify(r.response));
     // displayFileData(fileEntry.fullPath + " (content uploaded to server)");
 };

 var fail = function (error) {
     alert("An error has occurred: Code = " + error.code);
     console.log("Error= " + error);
 };

 var options = newFileUploadOptions();
  options.fileKey = "file";
  options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1);
  options.mimeType = "text/plain";

var params = {};
 params.value1 = "test";
 params.value2 = "param";

 options.params = params;

 varSERVER = "http://posttestserver.com/post.php";

 var ft = newFileTransfer();
 // SERVER must be a URL that can handle the request, like// http://some.server.com/upload.php
 ft.upload(fileURL, encodeURI(SERVER), success, fail, options);
}

when you get the success alert you can also check your params uploaded to the server or not, you will receive the link in success.

enter image description here

Hope this help.

Post a Comment for "Cordova/phonegap: Filetransfererror.file_not_found_err With File Transfer Plugin"