Save image from pouchDB to app data folder


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:
alt tag

public saveImage(imageFile): Promise<any> {
        let fileName = "test_file_name_4.jpg";

        return new Promise((resolve, reject) => {
                dirEntry => {
                    let isAppend = true;
                    createFile(dirEntry, fileName, isAppend);
                error => console.log('Load fs error error: ', JSON.stringify(error))

            function createFile(dirEntry, fileName, isAppend) {
                    {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([], {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));

                    if (isAppend) { // If we are appending data to file, go to the end of the file.
                        try {
                        catch (e) {
                            console.log("file doesn't exist!");


            function dataURItoBlob(image) {
                var byteString = unescape(;
                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?