From an API response I get JSON with file details.
In The JSON I have a property DocumentData which is a byte array.
What I need is to download that file in the device storage.
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?
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);
});
};
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!");
})