使用cordova插件文件传输时遇到了一些问题。那是我的代码:
window.requestFileSystem(
LocalFileSystem.PERSISTENT,
0,
function onFileSystemSuccess(fileSystem) {
fileSystem.root.getFile(
"dummy.html", {create: true, exclusive: false},
function gotFileEntry(fileEntry) {
var sPath = fileEntry.fullPath.replace("dummy.html", "");
var fileTransfer = new FileTransfer();
fileEntry.remove();
fileTransfer.download( 'http://cordova.apache.org/images/cordova_bot.png', sPath + photo.original_name,
function (theFile) {
alert('success: ' + JSON.stringify(theFile));
console.log("download complete: " + theFile.toURI());
// showLink(theFile.toURI());
},
function (error) {
alert('error: ' + JSON.stringify(error));
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code: " + error.code);
},
true
);
})
},
function (error) {
alert('error request: ' + JSON.stringify(error));
}
);
返回fileTransfer.download的错误回调,错误代码为3,http 401。 我已经更新了File和FileTransfer插件,我的cordova版本是4.3.0。 还检查了我的config.xml
<access origin="*" />
但它就在那里。 我尝试添加标题Connection:close,但没有结果。 尝试将下载的第4个参数设置为默认值(false) - 没有运气。
在Android平板电脑上进行测试。
有什么事吗?谢谢!
答案 0 :(得分:4)
刚刚找到一个&#34;解决方案&#34;我的问题。 我所做的是将文件传输插件版本从0.5.0降级到0.4.8。
如果有人遇到类似问题,请按以下步骤操作:
那就是它。似乎运作良好,至少返回成功回调,并没有真正测试更多。
答案 1 :(得分:1)
我个人不会创建文件然后删除它只是为了获取目录URL。您应该能够通过执行fileSystem.root.toURL()
来获得 - fileSystem.root是一个DirectoryEntry,因此包含您希望在DirectoryEntry上看到的方法。
快一点。
<强>更新强>
如果您倾向于使用文件删除方法,则应该在FileEntry上使用toURL(),而不是fullPath。我认为toURL()返回一个可以在整个HTML应用程序中使用的URL。
但正如我所说,fileSystem.root.toURL()更可取。示例代码如下:
因此,您的代码变为:
window.requestFileSystem(
LocalFileSystem.PERSISTENT,
0,
function (fileSystem) {
var url = 'http://cordova.apache.org/images/cordova_bot.png',
dir = fileSystem.root.toURL() + photo.original_name,
ft = new FileTransfer();
ft.download(url, dir,
function (fileEntry) {
alert('Downloaded!');
},
function (error) {
alert('Download error');
console.log(dir);
}
);
},
function (error) {
alert('Error getting file system');
}
);
试试看看会发生什么。可能听起来很愚蠢,但我认为photo.original_name
是定义的?目录和文件名之间有一个'/'?
答案 2 :(得分:0)
如果您的远程文件需要VPN访问,请确保您的设备已连接到VPN。
就我而言,我没有连接到设备上的VPN。
答案 3 :(得分:0)
只需将下载网址更改为 http://www。{{域名}} / abc.pdf
在www上使用完整网址
答案 4 :(得分:0)
这对我有用
let pathToDownload = `${cordova.file.externalDataDirectory}`;
let fileTransfer = new FileTransfer();
let url = encodeURI(`https://cordova.apache.org/images/cordova_bot.png`);
fileTransfer.download(url, pathToDownload + "image.png",
(fileEntry) => {
console.log('Downloaded!');
},
(error) => {
console.log('Download error');
},
);