gpt4 book ai didi

javascript - 如何使用 cordova filetransfer 下载 Base 64 图像

转载 作者:行者123 更新时间:2023-12-03 07:38:14 25 4
gpt4 key购买 nike

我使用了cordova文件传输协议(protocol)并使用了下载功能来下载base-64图像。当我将远程服务器路径(例如“https://upload.wikimedia.org/wikipedia/commons/4/4a/Logo_2013_Google.png”)放在文件路径上时,它会下载,但是当我将base-64图像pah放在文件路径上时,它不会下载。我对 Base-64 转换一无所知。请帮忙。

----我的代码如下----

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

var d = new Date();
var imageName = "sample" + d.getTime() + ".png";

//var filepath = encodeURI("https://upload.wikimedia.org/wikipedia/commons/4/4a/Logo_2013_Google.png");
var filepath = encodeURI(imageData);

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
fileSystem.root.getFile(imageName, { create: true, exclusive: true }, 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;
var fileTransfer = new FileTransfer();
fileTransfer.download(remoteFile, localPath, function (newFileEntry) {
// successful download, continue to the next image
console.log('successful download');
},
function (error) { // error callback for #download
console.log('Error with #download method.', error);
});
});
})

})
}
}

提前致谢。

最佳答案

我找到了自己问题的解决方案。

首先,我已将 Base64 图像转换为文件流,并使用 Web 服务在服务器上创建图像,并在服务器上上传图像,并获取该服务器路径并使用 cordova 文件传输进行下载。

我的代码如下

=> 用于将 Base64 图像转换为图像的 Web 服务代码

string strImg = imageData.imageData;
string fullName = "d:\\uploadimages";
FileStream fs = new FileStream(fullName, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);

byte[] data = Convert.FromBase64String(strImg);

bw.Write(data);
bw.Close();

=> 在手机上下载图片

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

})
}

关于javascript - 如何使用 cordova filetransfer 下载 Base 64 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35519505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com