How to download base-64 source images in mobile device(Android,iPhone,Windows) and save into device phone memory

755 views Asked by At

I have created one sample app using telerik app builder. I have created one simple view and one js file, render one image in view and convert it into base-64. I have requirement to download this image and save it to device internal storage using javascript

I have used download.js plugin of javascript but download functionality is not working.Please give suggestion.

My Code is below:

function (image)
{
var imageData = image.src;
imageData = imageData.replace('data:image/png;base64,', '');
download(imageData, "image11111", "image/png");
}
1

There are 1 answers

0
kapil darji On BEST ANSWER

I found my own question's answer.

You can download image in mobile device using cordova filetransfer.

function download()
{
    var filepath = encodeURI("http://www.telerik.com/sfimages/default-source/logos/app_builder.png"),
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
                fileSystem.root.getFile("sample.jpg", { create: true, exclusive: false }, function (fileEntry) {

                    // get the full path to the newly created file on the device
                    var localPath = fileEntry.fullPath;

                    // massage the path for android devices (not tested)
                    if (device.platform === "Android" && localPath.indexOf("file://") === 0) {
                        localPath = localPath.substring(7);
                    }

                    // download the remote file and save it
                    var remoteFile = filepath;
                    //loadingOverlay.displayLoading("Image will be save on your device.");

                    var fileTransfer = new FileTransfer();
                    fileTransfer.download(remoteFile, localPath, function (newFileEntry) {
                        // successful download, continue to the next image
                        var dwnldImagePath = newFileEntry.fullPath;
                        console.log('successful download');

                    },
                    function (error) { // error callback for #download
                        console.log('Error with #download method.', error);

                    });
                });
                function(error) { // error callback for #getFile
                    console.log('Error with #getFile method.', error);
                });

            })
}