Javascript 下载图像并本地保存在 iPhone Phonegap 应用程序上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3625640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Download images and save locally on iPhone Phonegap app
提问by StJimmy
I've already managed to save a web page (x/html) successfully, but I'd also like to save the images and mp4 videos that are contained in it, for further visualization in offline mode.
我已经成功地保存了一个网页 (x/html),但我还想保存其中包含的图像和 mp4 视频,以便在离线模式下进一步可视化。
I've got access to the iOS filesystem, so I save the html by obtaining the code through an AJAX request, and later saving it to a file.
我可以访问 iOS 文件系统,因此我通过 AJAX 请求获取代码来保存 html,然后将其保存到文件中。
I don't really know how to do the same with videoand images. I have a server to which I can send queries from my app, so it shows exclusively the content I need to download, with the optimal headers in case its necessary. I just don't know how to "download" it from the client side (Javascript).
我真的不知道如何对video和images做同样的事情。我有一个服务器,我可以从我的应用程序向它发送查询,因此它只显示我需要下载的内容,并在必要时提供最佳标头。我只是不知道如何从客户端(Javascript)“下载”它。
Thanks in advance for any help.
在此先感谢您的帮助。
回答by Mister Smith
You can use a FileTransferobject to download a remote image to a local file.
您可以使用FileTransfer对象将远程图像下载到本地文件。
This is the latest official sample snippet:
这是最新的官方示例片段:
// !! Assumes filePath is a valid path on the device
var fileTransfer = new FileTransfer();
var uri = encodeURI("http://some.server.com/download.php");
fileTransfer.download(
uri,
filePath,
function(entry) {
console.log("download complete: " + entry.fullPath);
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
},
false,
{
headers: {
"Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
}
}
);
回答by decodingpanda
I have used this snippet on my ios app project:
我在我的 ios 应用项目中使用了这个片段:
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, fail);
var target_directory="";
function fail() {
//alert("failed to get filesystem");
}
function downloadImage(url, filename){
alert("download just started.");
try{
var ft = new FileTransfer();
ft.download(
url,
target_directory + filename,
function(entry) {
//alert("download complete!:" + entry.nativeURL ); //path of the downloaded file
},
function(error) {
//alert("download error" + error.code);
//alert("download error" + JSON.stringify(error));
}
);
}
catch (e){
//alert(JSON.stringify(e));
}
}
function success(fileSystem) {
target_directory = fileSystem.root.nativeURL; //root path
downloadImage(encodeURI("http://upload.wikimedia.org/wikipedia/commons/2/22/Turkish_Van_Cat.jpg"), "cat.jpg"); // I just used a sample url and filename
}
回答by Shazron
You can only do it natively I'm afraid. I'm doing it through a FileDownload PhoneGap Plugin that I wrote, using NSURLConnection. I pass in the url to download to the plugin through Javascript, and a target location (and it even gives me download progress).
恐怕你只能在本地做到这一点。我是通过我编写的 FileDownload PhoneGap 插件使用 NSURLConnection 来实现的。我通过 Javascript 传入要下载到插件的 url 和目标位置(它甚至给了我下载进度)。
回答by koffster
Have not tested it yet, but the documentation at PhoneGap looks quite promising http://docs.phonegap.com/en/1.0.0/phonegap_file_file.md.html
还没有测试过,但 PhoneGap 的文档看起来很有前途http://docs.phonegap.com/en/1.0.0/phonegap_file_file.md.html

