7

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

LuckyStarr
  • 1,468
  • 2
  • 26
  • 39
Sagar Pudi
  • 4,634
  • 3
  • 32
  • 51
  • I am unable to send complex multipart/form-data request which containts multiple blobs while using Filetransfer.upload(). hence looking for alternatives. – Sagar Pudi Nov 13 '17 at 07:38

1 Answers1

-1

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!

taranjeetsapra
  • 524
  • 7
  • 19
  • 1
    I am not looking for displaying as an image. Please read the question carefully once again. – Sagar Pudi Nov 20 '17 at 07:07
  • I have posted an answer with Base64 method to display image because it is more generic and works in both android and iOS. Android cause some problem:For more recent apps targeting Android 7.0 (API level 24) and higher, passing a file:// URI across a package boundary causes a FileUriExposedException. Therefore in native android we get content:// uri rather than file. – taranjeetsapra Nov 20 '17 at 08:45