You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@cordova.apache.org by GitBox <gi...@apache.org> on 2020/12/16 22:17:49 UTC

[GitHub] [cordova-plugin-file] breautek commented on issue #448: How can i get the real js File Object from the localpath?

breautek commented on issue #448:
URL: https://github.com/apache/cordova-plugin-file/issues/448#issuecomment-747074073


   Unfortunately the `File` type is a special type that cannot be constructed manually, therefore Cordova cannot give you the "real" (browser native `File`) object. Only the browser itself can construct native `File` objects through certain browser APIs.
   
   A workaround is to read the file into a blob, which can be sent to the server in the same way as a `File` type can be. The native `File` type is a `Blob`, so any API that expects a `File` type will also work with a `Blob` type.
   
   see below for an example of how to read the file as a blob:
   
   ```js
   function readBinaryFile(fileEntry) {
   
       fileEntry.file(function (file) {
           var reader = new FileReader();
   
           reader.onloadend = function() {
   
               console.log("Successful file write: " + this.result);
               displayFileData(fileEntry.fullPath + ": " + this.result);
   
               var blob = new Blob([new Uint8Array(this.result)], { type: "image/png" });
               displayImage(blob);
           };
   
           reader.readAsArrayBuffer(file);
   
       }, onErrorReadFile);
   }
   ```


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org