Hi everyone!
For save file I use cordova-plugin-file.
Image from pouchDB successfully saved, but it’s broken and I can’t open it with any viewer (I copied the image on PC and tried to open it)
Also I try to save txt file with data and all works fine, so problem in saving image data.
Here is my method code:
imageFile is a file image object. It look’s like:

public saveImage(imageFile): Promise<any> {
let fileName = "test_file_name_4.jpg";
return new Promise((resolve, reject) => {
(<any>window).resolveLocalFileSystemURL(
cordova.file.dataDirectory,
dirEntry => {
let isAppend = true;
createFile(dirEntry, fileName, isAppend);
},
error => console.log('Load fs error error: ', JSON.stringify(error))
);
function createFile(dirEntry, fileName, isAppend) {
dirEntry.getFile(
fileName,
{create: true, exclusive: false},
fileEntry => writeFile(fileEntry, imageFile, isAppend),
error => console.log('Create file error: ', JSON.stringify(error))
);
}
function writeFile(fileEntry, imageFile, isAppend) {
// imageFile = dataURItoBlob(imageFile); // also try this code, but still save broken image
imageFile = new Blob([imageFile.data], {type: imageFile.content_type});
fileEntry.createWriter(fileWriter => {
fileWriter.onwriteend = () => {
console.log("Successful file read...");
// readFile(fileEntry);
resolve(cordova.file.dataDirectory + fileName);
};
fileWriter.onerror = (error) => {
console.log("Failed file WRITE: ", JSON.stringify(error));
reject(error);
};
if (isAppend) { // If we are appending data to file, go to the end of the file.
try {
fileWriter.seek(fileWriter.length);
}
catch (e) {
console.log("file doesn't exist!");
}
}
fileWriter.write(imageFile);
});
}
function dataURItoBlob(image) {
var byteString = unescape(image.data);
var ia = new Uint8Array(byteString.length);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ia], {type: image.content_type});
}
});
}
According to docs, for save file I need a location string, file name and Blob instance of image. I think, that problem in blob instance. I try to create it with two ways (second way commented in code).
Any ideas?