CordovaFileTransfer |如何将标题中的字符串放入TargetPath?

时间:2016-04-15 09:09:26

标签: javascript angularjs cordova ionic-framework cordova-plugins

我用Ionic Framework和AngularJS编写了项目。 他的任务是从URL下载文件,输入http://www.uzhnu.edu.ua/uk/infocentre/get/6500 问题是“如何下载文件并给它命名”的例子只有在URL行的路径有他的名字时才会出现。

我的文件名在url中没有此名称。它位于http请求的标题中,这里是用POSTMAN制作的屏幕,但是如何获取它并放入变量TargetPath,我不知道。谁能提出建议? 这是

的代码

FileTransferController.js:

app.controller('MyCtrl', function($scope, $cordovaFile, $cordovaDialogs, $window, $ionicLoading,$ionicPopup, $timeout, $cordovaFileTransfer) {

  $scope.id = '6500';

  $scope.downloadFile = function() {
    $ionicLoading.show({template: 'Download file...'});
    var url = "http://www.uzhnu.edu.ua/uk/infocentre/get/"+$scope.id;
    var filename = $scope.id+".doc";
    alert(filename);
    var targetPath = "/storage/sdcard0/documents/" + filename;
    var trustHosts = true;
    var options = {};
    $cordovaFileTransfer.download(url, targetPath, options, trustHosts)
      .then(function(entry) {
          // Success!
          $ionicLoading.hide();
          console.log('download complete: ' + entry.toURL());
          alert('File download: ' + targetPath);
        }, function(error) {
          $ionicLoading.hide();
          // An error occured. Show a message to the user
          alert('Sorry');
          alert(JSON.stringify(error));
          console.log("download error source " + error.source);
          console.log("download error target " + error.target);
          console.log("upload error code" + error.code);
        },
        false,
        {
          headers: {
            "Content-Disposition": ""
          },
        });
  };
});

app.js:

var app = angular.module('starter', ['ionic', 'ngCordova'])
  .run(function($ionicPlatform) {
    $ionicPlatform.ready(function() {
      if(window.cordova && window.cordova.plugins.Keyboard) {
        cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      }
      if(window.StatusBar) {
        StatusBar.styleDefault();
      }
    });
  })

的index.html:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
  <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
  <title></title>

  <link href="lib/ionic/css/ionic.css" rel="stylesheet">
  <link href="css/style.css" rel="stylesheet">
  <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
  <link href="css/ionic.app.css" rel="stylesheet">
  -->
  <!-- ionic/angularjs js -->
  <script src="lib/ionic/js/ionic.bundle.js"></script>
  <!-- ng-cordova  -->
  <script src="js/ng-cordova.js"></script>
  <script src="js/ng-cordova.js"></script>
  <script src="js/ng-cordova.min.js"></script>
  <!-- cordova script (this will be a 404 during development) -->
  <script src="cordova.js"></script>
  <!-- your app's js -->
  <script src="js/app.js"></script>
  <script src="js/FileTransferController.js"></script>
</head>
<body ng-app="starter">
<ion-pane>
  <ion-header-bar class="bar-stable">
    <h1 class="title">Ionic Blank Starter</h1>
  </ion-header-bar>
  <ion-content ng-controller="MyCtrl">
    <button class="button-positive" ng-click="downloadFile()">Download File</button>
  </ion-content>
</ion-pane>
</body>
</html>

P.S。代码仅适用于真实设备。并且必须下载所有插件:

离子插件添加cordova-plugin-file

离子插件添加cordova-plugin-file-transfer

并在文件夹“js”中添加文件“ng-cordova.js”和“ng-cordova.min.js”。他们可以使用:

bower安装ngCordova

1 个答案:

答案 0 :(得分:1)

YES!最后我做到了!如果有人需要代码,她的代码是:

 $scope.downloadFile = function() {
    var url = "http://example.com/page";
    $ionicLoading.show({template: 'Download file...'});
    $http.get(url).
      success(function (data, status, headers) {
        var head = headers('Content-Disposition');
        var filename = head.substr(head.lastIndexOf('=')+1);
        alert(filename);
        var targetPath = "/storage/sdcard0/documents/" + filename;
        var trustHosts = true;
        var options = {};
        $cordovaFileTransfer.download(url, targetPath, options, trustHosts)
          .then(function(entry) {
            $ionicLoading.hide();
            console.log('download complete: ' + entry.toURL());
            alert('File download: ' + targetPath);
          }, function(error) {
            $ionicLoading.hide();
            console.log('headers: ' + headers('Cache-Control'));
            // An error occured. Show a message to the user
            alert('Sorry');
            alert(JSON.stringify(error));
          })
        alert(head1);
        $ionicLoading.hide();
        $scope.$broadcast('scroll.refreshComplete');
        return(JSON.stringify(head1))
      })
      .error(function (status) {
        alert(status);
        $ionicLoading.hide();
        $scope.$broadcast('scroll.refreshComplete');
      });
  };