ionic framework windows phone, image/pdf file download and store it in phone memory

We creating a hybrid app using ionic framework for android, ios, and windows.

in that we have requirement like file download and store it in phone/sd card memory.

Basically we are getting base64 data from restApi, that we are converting to blob array. like this example Cordova-Android-Camera: How to save base64 encoded jpg to Android file system.

we are able to convert base64 to blob and save the file by using this code FileWriter.write(blob) in android and ios.

but windows phone i think it doesn't support blob conversion and also in this link https://github.com/apache/cordova-plugin-file they mentioned that cordova-plugin-file doen't support FileReader.readAsArrayBuffer nor FileWriter.write(blob).

is there any method we can use for windows phone to convert base64 to file.

we also tried to save image directly from some link but i think we required write permission to store files in windows, we don't know where to set write permission for windows phone.

Upvotes: 0

Views: 1071

Answers (1)

Naitik
Naitik

Reputation: 1465

I did already in one of my app and it's working great. Please do following:

Write code in js:

if(window.FileTransfer){
            try{
                console.log("filesys " + filesys);
                console.log("filesys root " + filesys.root);
                console.log("filesystem root path " + filesys.getRootPath);
                var filePath = filesys.root().toURL() + "/" + fileName;
                console.log("downloading file " + JSON.stringify(remoteData) + " at " + filePath);

                var fileTransfer = new FileTransfer();
                fileTransfer.download(
                    uri,
                    filePath,
                    function(entry) {
                        var retEntry = function(entry){
                            device.hideSpinner();
                            console.log("file downloaded " + entry.toURL());
                            success(entry);
                        }

                        retEntry(entry);
                    },
                    function(error) {
                        device.hideSpinner();
                        console.log("download error source " + error.source);
                        console.log("download error target " + error.target);
                        console.log("upload error code" + error.code);
                        error(error);
                    }
                );
            }catch(e){
                device.hideSpinner();
                device.alert("error " + e);
            }           }else{
            device.hideSpinner();

            console.log("opening the file");
            error();
            window.open(uri, '_blank', 'location=yes');             }

Which is normal file download code.

Please install following plugin to make it work.

1) cordova-plugin-file "File" https://github.com/apache/cordova-plugin-file

2) cordova-plugin-file-transfer "File Transfer" https://github.com/apache/cordova-plugin-file-transfer

3) cordova-plugin-inappbrowser "InAppBrowser" https://github.com/apache/cordova-plugin-inappbrowser

Hope it will help you.

Upvotes: 1

Related Questions