如何使用科尔多瓦文件传输下载 base 64 映像
How to download base 64 image using cordova filetransfer
我使用了cordova文件传输协议,并使用下载功能下载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);
});
})
}
相关文章:
- 如何在生成下载文件时显示加载动画
- 直接下载文件,而不是从window.open(url)
- 如何使用javascript或html下载PDF格式的填写表单
- Javascript运行php文件,然后下载文件
- 在单击href链接的同时下载文件
- 尽管链接成功并已成功下载,但未找到NPM模块
- 通过php页面中的js强制下载txt
- 使用angularjs向浏览器发送servlet响应(下载功能)
- 通过javascript下载文件时设置文件名
- 如何在Edge中下载图像/png数据URI
- 我的mp3链接被下载,而不是在我的网络播放器上流式传输
- 如何使用科尔多瓦文件传输下载 base 64 映像
- 仅从客户端检测下载开始/文件传输的开始
- 如何将文件流式传输到服务器,并同步地将其流式传输给用户?(通常文件下载模仿)
- 下载进度事件在使用 cordova 插件文件传输的 ios 中不起作用
- 是否有技术可以让设备访问和使用数据,而无需下载或流式传输
- Phonegap文件传输下载
- 流式传输文件而不下载
- 英特尔XDK保存pdf到存储卡文件传输.下载- android
- 如何将AJAX输出流式传输到用Javascript下载的文件中