How to get the file object on success callback of cordova camera?

Viewed 609

Currently I am using the following approach where it is giving details of file but not the actual object it seems like one we get from in javascript/jQuery. Does any one having the clue how to get the file object from File URI /native URI from mobile ios/android filesystem using cordova and javascript?

Below is the snippet I am using currently..

window.resolveLocalFileSystemURL(
  filepath,
  function(fileEntry) {
    fileEntry.file(
      function(file) {
        var reader = new FileReader();
        reader.onloadend = function() {
          var imgBlob = new Blob([this.result], {type: "image/jpeg"});
          var uploadedFile = imgBlob;
          uploadedFile.name = file.name;
          alert("Importing Asset from Camera.. " + uploadedFile.name);
          alert(uploadedFile.type);
          alert(uploadedFile.size);
          importAsset(uploadedFile);
        };
        reader.readAsArrayBuffer(file);
      },
      function(error) { alert("Error in fileEntry.file():" + error) })
  },
  function(error) { alert("Error in window.resolveLocalFileSystemURL():" + error) }
);

Note: FileTransfer.upload() will not work in my case.

Above snippet I am using after taking from Append image file to form data - Cordova/Angular after going through the existing Q&A from #SO

1 Answers

Base64 Image Upload will work both in iOS/Android as I have tried: As for iOS There is only one way to show image is using base64 format.You can use cordova-camera-plugin to to generate base64 image and put on img tag of HTML.I have set the flag to get data in jpeg,base64 in camera plugin.

<img id="cardImg">

Store the base64 image in javascript variable as:

var imgdata=data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==

Then Simply pass data to img tag of HTML as:

cardImg.src=imgdata;

Done!

Related