如何在移动设备(Android,iPhone,Windows)中下载base-64源图像并保存到设备手机内存中

时间:2016-02-19 11:14:43

标签: android ios asp.net download telerik-appbuilder

我使用telerik app builder创建了一个示例应用程序。我创建了一个简单的视图和一个js文件,在视图中渲染一个图像并将其转换为base-64。我要求下载此图像并使用javascript将其保存到设备内部存储

我使用了javascript的download.js插件,但下载功能不起作用。请给出建议。

我的代码如下:

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

1 个答案:

答案 0 :(得分:0)

我找到了自己的问题的答案。

您可以使用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);
                });

            })
}