1

我使用 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");
}
4

1 回答 1

0

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

您可以使用 cordova 文件传输将图像下载到移动设备中。

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);
                });

            })
}
于 2016-02-23T07:16:49.043 回答