简体   繁体   English

从Cordova的FileTransfer下载不完整的文件

[英]Incomplete file download from FileTransfer in Cordova

I am attempting to create an app using Phonegap Build while making use of several plugins in order to download an online file to the device's SD card. 我正在尝试使用Phonegap Build创建一个应用程序,同时利用几个插件将一个在线文件下载到设备的SD卡。 Making use of the following links: 利用以下链接:

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 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

I was able to get get this code to work: 我能够得到这个代码:

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

} }

HOWEVER the problem is that although getting a successCallBack, the file is only partially downloaded . 然而 ,问题是,虽然得到一个successCallBack,该文件只部分下载 Eg.ics-android.png : this has a file size of 14.7kB, yet the result has a size of 0B. Eg.ics-android.png:这个文件大小为14.7kB,但结果的大小为0B。 Another example of a 125kB file results in the download of an 104.55kB. 另一个125kB文件的例子导致下载104.55kB。 My config.xml has the following permissions: 我的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="*" />  

I've tried searching for an answer but so far it doesn't seem that much others have encountered this error. 我试过寻找答案,但到目前为止,似乎没有其他人遇到过这个错误。 Where have I gone wrong?/What have I done incorrectly? 我哪里出错?/我做错了什么?

Managed to get this to work for me: 管理让这个为我工作:

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=="
            //}
        }
    );

    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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