如何使用文件传输下载离子和cordova中的文件

时间:2016-04-09 10:05:44

标签: cordova ionic-framework

我是cordova和ion的新人

我想从服务器下载音频文件

  • 离子版本是:1.7.14
  • cordova版本是:6.1.0
  • android版本是:6

这是我的代码:

$scope.DownloadFile = function(url,filename)
{

var ft = new FileTransfer();
ft.download(
  url + filename, // what u download
  "/sdcard/" + filename , // this is the filename as well complete url
  // fileSystem.root.toURL() +  filename ,  // use ios and others
  function(entry) {
    alert("success");
    alert(JSON.stringify(entry));

  },
  function(err) {
    alert(url + filename);
    alert(JSON.stringify(err));
  }
  );
}

我的变量(url和filename)有效。

我的设备上的

返回此错误:

  

{"代码":空,"源":空,"目标":空," HTTP_STATUS":空,&# 34;主体":空," exeption":空}

下载链接服务器:

http://sedaban.com/sedaban/users/json/app/mobile/download/145836602113212.mp3

2 个答案:

答案 0 :(得分:1)

安装ngCordovacordova-plugin-file-transfer插件

创建一个像这样的函数,

$scope.fileDownload = function() {
        var url = "http://www.podtrac.com/pts/redirect.mp3/traffic.libsyn.com/ariynbf/AR_052316_Travis_McElroy.mp3";
        var filename = url.split("/").pop();

        var targetPath = cordova.file.externalRootDirectory + filename;

        $cordovaFileTransfer.download(url, targetPath, {}, true).then(function(result) {
            //console.log('Success');
            $scope.hasil = 'Save file on ' + targetPath + ' success!';
            $scope.mywallpaper = targetPath;
            alert('Your download is completed');
        }, function(error) {
            //console.log('Error downloading file');
            $scope.hasil = 'Error downloading file...';
            alert('Your download is failed');
        }, function(progress) {
            console.log('progress');
            $scope.downloadProgress = (progress.loaded / progress.total) * 100;
            // var downcountString = $scope.downloadProgress.toFixed();
            // console.log('downcountString');
            // $scope.downloadCount = downcountString;
        });
    }

不要忘记在控制器中定义$cordovaFileTransfer

然后通过执行类似的操作,单击调用该函数,

<button class="button button-small button-light" ng-click="fileDownload()"><i class="icon ion-android-download"></i> Download </button>

注意:某些设备可能会要求您启用存储权限以进行下载。在这种情况下,请确保在应用设置内打开“存储”切换按钮(设置&gt;应用设置&gt;权限&gt;存储)

答案 1 :(得分:0)

首先,确保您已安装cordova-plugin-file-transfer插件:

$ cordova plugin add cordova-plugin-file-transfer

然后确保在设备准备好后下载文件:

$scope.DownloadFile = function (url, filename) {
   var ft = new FileTransfer();
   var source = encodeURI(url + filename);
   var targetPath = cordova.file.documentsDirectory + filename;//change path as you need
   var trustHosts = true;//optional
   var options = {};//optional
   ionic.Platform.ready(function () {//device prepared
     ft.download(source, targetPath, function (entry) {
       console.log('success' + JSON.stringify(entry));
     }, function (err) {
       console.log('failed' + JSON.stringify(err));
     }, trustHosts, options);
   });
}

关于cordova-plugin-file-transfer插件的更多信息,请参阅https://github.com/apache/cordova-plugin-file-transfer。或者,如果您想更舒适地使用此插件,请尝试ngCordova并参阅http://ngcordova.com/docs/plugins/fileTransfer/。 希望这会对你有所帮助,问候!