【问题标题】:Incomplete file download from FileTransfer in Cordova从 Cordova 中的 FileTransfer 下载文件不完整
【发布时间】:2017-03-05 01:22:19
【问题描述】:

我正在尝试使用 Phonegap Build 创建应用程序,同时使用多个插件将在线文件下载到设备的 SD 卡。使用以下链接:

i) https://www.tutorialspoint.com/cordova/cordova_file_transfer.htm ii)https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file-transfer/index.html#download

我能够让这段代码工作:

function downloadFile() {


 var fileTransfer = new FileTransfer();
  fileTransfer.download(
"http://developer.android.com/assets/images/home/ics-android.png",
"file:///storage/sdcard0/aw2uin.png",
function(entry) {
    alert("download complete: 1" + entry.toURL);
},
function(error) {
    alert("download error source " + error.source);
    alert("download error target " + error.target);
    alert("upload error code" + error.code);
});

}

但是问题在于,虽然获得了successCallBack,但文件只是部分下载。 eg.ics-android.png :文件大小为 14.7kB,但结果大小为 0B。 125kB 文件的另一个示例导致下载 104.55kB。 我的 config.xml 有以下权限:

<plugin name="cordova-plugin-file" spec="~4.3.1" />
<plugin name="cordova-plugin-file-transfer" spec="~1.6.1" />
<plugin name="cordova-plugin-network-information" spec="~1.3.1" />
<plugin name="cordova-plugin-whitelist" version="1.3.1" />
<access origin="*" /><!--subdomains="true" /> -->
<preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,assets,root" />
<preference name="android-minSdkVersion" value="7" />
<preference name="android-installLocation" value="preferExternal" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-navigation href="*" />  

我已尝试寻找答案,但到目前为止似乎没有多少其他人遇到此错误。我哪里做错了?/我做错了什么?

【问题讨论】:

    标签: android cordova phonegap-build file-transfer


    【解决方案1】:

    设法让它为我工作:

    function downloadFile() {
    
    
     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
    
    alert('file system open: ' + fs.name);
    
    
    var url = 'https://....';
    // Parameters passed to getFile create a new file or return the file if it already exists.
    fs.root.getFile('downloaded-image.png', { create: true, exclusive: false }, function (fileEntry) {
        download(fileEntry, url, true);
    
    }, alert("fail1"));
    }, alert("fail2"));
    
    }
    
    function download(fileEntry, url, readBinaryData) {
    
        var fileTransfer = new FileTransfer();
        var fileURL = cordova.file.externalRootDirectory + "filename.png";  
    
        fileTransfer.download(
            url,
            fileURL,
            function (entry) {
                alert("Successful download...");
                alert("download complete: " + entry.toURL());
    
                if (readBinaryData) {
                  // Read the file...
                  readBinaryFile(entry);
                }
                else {
                  // Or just display it.
                  displayImageByFileURL(entry);
                }
            },
            function (error) {
                alert("download error source " + error.source);
                alert("download error target " + error.target);
                alert("upload error code" + error.code);
            },
            null, // or, pass false
            {
                //headers: {
                //    "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
                //}
            }
        );
    
        };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-30
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-20
      相关资源
      最近更新 更多