在我在Android 4.4上运行phonegap cordova 3.5的应用程序中,使用文件传输插件上传文件效果很好,但是下载会产生异常。我在设备上和模拟器上测试了blueStacks 这个代码适用于cordova 2.9但是当我更新cordova到3.5下载时会产生异常
下载功能:
download = function () {
viewModel.popup_download.visible(false);
loadPanelVisible(true);
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
fileSystem.root.getFile(viewModel.file_download_name(), {create: true, exclusive: false}, function() {
var ft = new FileTransfer();
uri="https://api.exemple-exemple.com/api/Dav/Download/"+path+"/"+viewModel.folder_downlod();
filePath=currPath+"/"+viewModel.file_download_name();
ft.download(uri,filePath,win_download,fail_download,false, {
headers: {
"HeaderUserData": JSON.stringify({ ID: id, Token: token }),
"HeaderDavData": JSON.stringify({ UserName: viewModel.userId_dav(), Password: viewModel.userPassword_dav() }),
}
});
}, fail);
}, fail);
}
答案 0 :(得分:3)
你在空格和特殊字符中遇到问题,你必须在发送请求之前使用encodeURI。
download = function () {
viewModel.popup_download.visible(false);
loadPanelVisible(true);
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
fileSystem.root.getFile(viewModel.file_download_name(), {create: true, exclusive: false}, function() {
var ft = new FileTransfer();
uri="https://api.phosphorus-technologies.com/api/Dav/Download/"+path+"/"+viewModel.folder_downlod();
filePath=currPath+"/"+viewModel.file_download_name();
uri = encodeURI(uri);
ft.download(uri,filePath,win_download,fail_download,false, {
headers: {
"HeaderUserData": JSON.stringify({ ID: id, Token: token }),
"HeaderDavData": JSON.stringify({ UserName: viewModel.userId_dav(), Password: viewModel.userPassword_dav() }),
}
});
}, fail);
}, fail);}
答案 1 :(得分:0)
变量currPath永远不会被定义,我认为你根本不想使用它。您从viewModel传递了下载文件夹,然后在回调中获取文件夹结果。
download = function () {
viewModel.popup_download.visible(false);
loadPanelVisible(true);
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
fileSystem.root.getFile(viewModel.folder_downlod(), {create: true, exclusive: false}, function(filePath) { // ADD PARAMETER HERE
var ft = new FileTransfer();
uri="https://api.phosphorus-technologies.com/api/Dav/Download/"+path+"/"+viewModel.folder_downlod(); //IS THIS A VALID URL ENDING IN THE FOLDER NAME?
//REMOVE FILE PATH HERE
ft.download(uri,filePath,win_download,fail_download,false, {
headers: {
"HeaderUserData": JSON.stringify({ ID: id, Token: token }),
"HeaderDavData": JSON.stringify({ UserName: viewModel.userId_dav(), Password: viewModel.userPassword_dav() }),
}
});
}, fail);
}, fail);
}
我担心您构建以文件夹结尾的API调用,因为此代码仅用于下载单个文件。我认为可能只是命名不一致。