Download / create file from byte array

Hey Guys

I need to achieve the following:

  1. From an API response I get JSON with file details.
  2. In The JSON I have a property DocumentData which is a byte array.
  3. What I need is to download that file in the device storage.
  4. I can create a file with the cordova-file-plugin but I can’t load it with the DocumentData, as the cordova file plugin receives a string for the data property.

So My questions are. How can I create / Download a file from a byte array?
Can I achieve this with the cordova-file-plugin, or I need to use other plugin?

Regards
Jordan

Have you checked that your bytes are correctly transferred?
JSON byte array could be base64 encoded, you need to decode it before saving it to file.

The problem was that the method from the plugin is receiving a string, so it was writing the byte[] to the file as string.

I solved the problem by converting the byte[] to binary array first using this snippet:

var byteArr=[12,-123,43,99, …] ;
var UTF8_STR = new Uint8Array(byteArr); // Convert to UTF-8…
var BINARY_ARR=UTF8_STR.buffer; // Convert to buffer…

The source is from Here: http://stackoverflow.com/questions/19657001/how-to-write-utf-8-or-base64-data-into-a-file-jpg-doc-pdf-on-local-storagesdc

Hi @joconolimits @gmarziou ,

My requirements also same as yours. I have followed the same steps, you mentioned, I am able to get the content of .txt files but .docx & .pdf files are showing nothing when open.

Please find my code below.

It would be very helpful if you provide me some solution.

Thanks in advance.
$scope.createFile = function(data,title){

	var UTF8_STR = new Uint8Array(data); // Convert to UTF-8... 
	var BINARY_ARR = UTF8_STR.buffer; // Convert to buffer...
	
		$cordovaFile.writeFile(cordova.file.dataDirectory, title, BINARY_ARR, true)
			  .then(function (success) {
				// success
				console.log("File created successfully");
				}, function (error) {
				// error
				console.log(error);
		});
};

Hi @Hariharan,

Did you find any solution…Please share.I am also facing problem when trying to save as Pdf file.

Thanks in advance.

What type of data is your “data” variable that you pass here: var UTF8_STR = new Uint8Array(data);
It needs to be a byte array.
The same worked for me with various file types like .pdf, .doc .jpg

@joconolimits
I basically want to convert Image URI to .pdf file.

So first I am converting an Image URI to Character Array and then I am converting it into binary.

After that I am using Write File API to create .pdf file.

After doing this code. I am facing an issue that when WriteFile gives me success and I open .pdf file through fileOpener or manually then it gives me error that Invalid Format.

This error only comes when I generate .pdf file.

Do you know how to solve it.

 this.screenshot.URI(80).then(succ=>{
        var byteCharacters = decodeURI(succ.URI)
         var byteNumbers = new Array(byteCharacters.length);
          for (var i = 0; i < byteCharacters.length; i++) {
              byteNumbers[i] = byteCharacters.charCodeAt(i);
          } 
          var byteArray = new Uint8Array(byteNumbers);
          var BINARY_ARR = byteArray.buffer;
   

         console.log("BINARY_ARR : ",BINARY_ARR )
          this.createFile(BINARY_ARR);
        }
      ,() => {
            this.showToast("There is an error!");
          })
  this.file.writeFile(cordova.file.externalDataDirectory,this.createFileName(),blob,true).then(succ=>{
      console.log("File write success : ", succ)
      this.fileOpener.open(
      succ.nativeURL,
      'application/pdf'
    ).then((success) => {
      console.log('success open file: ', success);
    }, (err) => {
      console.log('error open file', err);
    });

    },
    err=>{
      console.log(" write File error : ", err)
    })