我试图通过使用phonegap文件传输插件下载方法从URL下载图像
但总是得到错误代码:3,
这是我的代码:
window.requestFileSystem(window.LocalFileSystem.PERSISTENT, 0, function(fs)
{
// create the download directory is doesn't exist
fs.root.getDirectory('download', { create: true });
// save file in .. download
var filePath = fs.root.fullPath + 'download/' + url.split('/').pop();
var fileTransfer = new FileTransfer();
var uri = encodeURI(decodeURIComponent("http://phonegap.com/uploads/artwork/Build-Bot-Preview.png"));
filePath="file:///sdcard"+filePath;
fileTransfer.download(uri,filePath,function(entry)
{
alert("Successfully downloaded file, full path is " + entry.fullPath);
},
function(error)
{
alert("Some error " + error.code + " for " + url);
},
false);
});
感谢。
答案 0 :(得分:0)
我在新创建的phonegap项目中尝试了您的代码,它对我有用。我得到an alert表示它成功了。
<强> index.js:强>
onDeviceReady: function() {
app.receivedEvent('deviceready');
var url = "http://animalia-life.com/image.php?pic=/data_images/dog/dog1.jpg";
window.requestFileSystem(window.LocalFileSystem.PERSISTENT, 0, function(fs)
{
// create the download directory is doesn't exist
fs.root.getDirectory('download', { create: true });
// save file in .. download
var filePath = fs.root.fullPath + 'download/' + url.split('/').pop();
var fileTransfer = new FileTransfer();
var uri = encodeURI(decodeURIComponent("http://phonegap.com/uploads/artwork/Build-Bot-Preview.png"));
filePath="file:///sdcard"+filePath;
fileTransfer.download(uri,filePath,function(entry)
{
alert("Successfully downloaded file, full path is " + entry.fullPath);
},
function(error)
{
alert("Some error " + error.code + " for " + url);
},
false);
});
},
我希望它有所帮助。至少你知道代码现在正在运行。
修改强>
你的config.xml中是否有访问源行?:
<access origin="*" />
编辑:编辑:
我在config.xml中声明了这些行的插件:
<gap:plugin name="cordova-plugin-file" source="npm" version="3.0.0" />
<gap:plugin name="cordova-plugin-file-transfer" source="npm" version="1.3.0" />