CordovaFile& CordovaFileTransfer。如何使用正确的类型文件获取正确的文件名

时间:2016-04-14 10:56:44

标签: angularjs ionic-framework cordova-plugins ngcordova

我有一个从网站加载文件的项目。在浏览器中,单击链接(http://www.uzhnu.edu.ua/uk/infocentre/get/6500),它会立即加载文件,并且名称和扩展名不正确。如何在离子应用程序中实现它,我不知道。 从url下载的文件,其中$ scope.id是文件的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() {
// Success!
$ionicLoading.hide();
alert('File download ' + targetPath);
}, function(error) {
$ionicLoading.hide();
// An error occured. Show a message to the user
alert('Sorry');
alert(JSON.stringify(error));
});
};

2 个答案:

答案 0 :(得分:0)

使用以下功能从服务器下载文件

将参数传递给函数 document_filename 并在 $ scope.id

的位置使用它
        $scope.downloadFile = function(document_filename) {
            $ionicLoading.show({
                template: 'Download file...'
            });
            var url = "http://www.uzhnu.edu.ua/uk/infocentre/get/" + document_filename;
            var filename = document_filename + ".doc";
            alert(filename);
            var targetPath = "/storage/sdcard0/documents/" + filename;
            var trustHosts = true
            var options = {};
            $cordovaFileTransfer.download(url, targetPath, options, trustHosts)
                .then(function() {
                    // Success!
                    $ionicLoading.hide();
                    alert('File download ' + targetPath);
                }, function(error) {
                    $ionicLoading.hide();
                    // An error occured. Show a message to the user
                    alert('Sorry');
                    alert(JSON.stringify(error));
                });
        };

希望这会对你有所帮助!!

有关详细信息,请转到here

答案 1 :(得分:0)

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