PhoneGap 将图像下载到特定文件夹
PhoneGap download image to a specific folder
我正在为 iOS 平台构建基于 PhoneGap 的应用程序。
我想将几个图像文件下载到特定目录。
我想将下载的图片存储在文件夹 www/my_img/
以便在我的应用程序中我可以进一步将此图片用作:
<img src="my_img/downloaded.jpg" width="100px" height="100px">
我正在使用 PhoneGap 插件下载图片:
var url = 'http://myServer.com/img.jpg';
var filePath = 'www/my_img/';
var fileTransfer = new FileTransfer();
var uri = encodeURI(url);
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: {
}
}
);
但问题是图像没有保存在指定的文件夹中。
如何将下载的图像保存在 "www/my_img/" 文件夹中?
我认为您应该先访问文件系统。确定后,您可以在您有权访问的目录中创建的文件夹中下载图像。如果你需要,我可以给你一个片段。
编辑:
1) 访问文件系统:
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFSCategory, fail);
}
2) 如果有文件系统,获取主目录:
function gotFSCategory(fileSystem) {
window.fileSystem = fileSystem;
fileSystem.root.getDirectory(window.appRootDirName, {
create : true,
exclusive : false
}, dirReadyCategory, fail);
}
3) 主目录准备好后,保存并继续:
function dirReadyCategory(entry) {
window.appRootDir = entry;
console.log('application dir is ready with window.appRootDir: '+JSON.stringify(window.appRootDir));
// Start my code:
start_my_code();
}
至于文件路径变量,我使用这个(每个文件一个):
var filePath = window.appRootDir.toURL() + fileName;
问题出在 filePath
的值中。这需要是 device-absolute-file-path 或 filesystem URL.
看看 comptibility notes part of the docs:
您可以使用一些预定义的文件夹。我认为最适合你的情况(它是 r/w,不需要设置任何权限并且是持久的)是:
cordova.file.dataDirectory
您可以将下载的图像存储在那里,完成后,设置图像 src。
将此翻译成您的情况:
HTML
<img id="downloadedimage" width="100px" height="100px">
JS
var url = 'http://myServer.com/img.jpg';
var filePath = cordova.file.dataDirectory + '/img.png';
var fileTransfer = new FileTransfer();
var uri = encodeURI(url);
fileTransfer.download(
uri,
filePath,
function(entry) {
console.log("download complete: " + entry.fullPath);
document.getElementById("downloadedimage").src = entry.toURL();
},
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: {
}
}
);
我正在为 iOS 平台构建基于 PhoneGap 的应用程序。
我想将几个图像文件下载到特定目录。
我想将下载的图片存储在文件夹 www/my_img/
以便在我的应用程序中我可以进一步将此图片用作:
<img src="my_img/downloaded.jpg" width="100px" height="100px">
我正在使用 PhoneGap 插件下载图片:
var url = 'http://myServer.com/img.jpg';
var filePath = 'www/my_img/';
var fileTransfer = new FileTransfer();
var uri = encodeURI(url);
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: {
}
}
);
但问题是图像没有保存在指定的文件夹中。
如何将下载的图像保存在 "www/my_img/" 文件夹中?
我认为您应该先访问文件系统。确定后,您可以在您有权访问的目录中创建的文件夹中下载图像。如果你需要,我可以给你一个片段。
编辑:
1) 访问文件系统:
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFSCategory, fail);
}
2) 如果有文件系统,获取主目录:
function gotFSCategory(fileSystem) {
window.fileSystem = fileSystem;
fileSystem.root.getDirectory(window.appRootDirName, {
create : true,
exclusive : false
}, dirReadyCategory, fail);
}
3) 主目录准备好后,保存并继续:
function dirReadyCategory(entry) {
window.appRootDir = entry;
console.log('application dir is ready with window.appRootDir: '+JSON.stringify(window.appRootDir));
// Start my code:
start_my_code();
}
至于文件路径变量,我使用这个(每个文件一个):
var filePath = window.appRootDir.toURL() + fileName;
问题出在 filePath
的值中。这需要是 device-absolute-file-path 或 filesystem URL.
看看 comptibility notes part of the docs:
您可以使用一些预定义的文件夹。我认为最适合你的情况(它是 r/w,不需要设置任何权限并且是持久的)是:
cordova.file.dataDirectory
您可以将下载的图像存储在那里,完成后,设置图像 src。
将此翻译成您的情况:
HTML
<img id="downloadedimage" width="100px" height="100px">
JS
var url = 'http://myServer.com/img.jpg';
var filePath = cordova.file.dataDirectory + '/img.png';
var fileTransfer = new FileTransfer();
var uri = encodeURI(url);
fileTransfer.download(
uri,
filePath,
function(entry) {
console.log("download complete: " + entry.fullPath);
document.getElementById("downloadedimage").src = entry.toURL();
},
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: {
}
}
);