简体   繁体   中英

Phonegap fileTransfer.Download is not working error code 3

In my apps running phonegap cordova 3.5 on android 4.4, using the file-transfer plugin to upload files works well but dowloading create an exception. I tested on the device and on the emulator blueStacks this code works fine with cordova 2.9 but when I update the cordova to 3.5 dowloading create an exception

the download function:

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); 
}

you got problem in spaces and special chars, you must use encodeURI before you send the request.

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);} 

The variable currPath is never defined, and I don't think you want to use it at all. You passed your download folder from the viewModel, so then get the folder result in the callback.

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); 
}

It worries me that you construct your API call ending with a folder, because this code will only work to download a single file. I think it may just be that the naming is inconsistent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM