javascript - Cordova 文件传输 |如何将标题中的字符串放入 TargetPath?

标签 javascript angularjs cordova ionic-framework cordova-plugins

我有使用 Ionic Framework 和 AngularJS 编写的项目。 他的任务是从 URL 下载文件,输入 http://www.uzhnu.edu.ua/uk/infocentre/get/6500 问题是,“如何下载文件并为其命名”的示例仅适用于 URL 行的路径有他的名字的情况。

我的文件名在 url 中没有这个名称。它位于http请求的 header 中,这是用 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();
      }
    });
  })

索引.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>

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

ionic 插件添加 cordova-plugin-file

ionic 插件添加 cordova-plugin-file-transfer

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

凉亭安装 ngCordova

最佳答案

是的!我终于做到了!如果有人需要代码,她的 ir 是:

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

关于javascript - Cordova 文件传输 |如何将标题中的字符串放入 TargetPath?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36642898/

相关文章:

javascript - 为页面加载时不存在的内容设置动画

javascript - jQuery ui slider 错误的 ui 值

javascript - AngularFire 0.5 $删除。错误 : Firebase. 子项失败:第一个参数是无效路径

android - Android 中的 float Phonegap Activity

javascript - Framework7 上未显示涟漪效果和图标

javascript - 触发器 i :active when keydown on HTML page

javascript - 更改代表获取行 ID 的按钮名称

angularjs - 在angularjs路由中更改路由路径而不重新加载 Controller 和模板

javascript - 在 Controller 之间传递id值

android - 使用 ionic-cordova 调试 android 应用程序