ionic 通过url下载文件到本地

方法多种多样。

  1. [使用 文件管理插件] (http://www.ionic.wang/ng_cordova-file.html)
    cordova plugin add cordova-plugin-file
    2.controller.js 添加方法。
//2 new 接单后,点击某订单,显示订单的情况
.controller('DownCtrl',  function($scope,$timeout,$cordovaFileTransfer){
      $scope.downloadFile = function(pathurl) {
              var url = pathurl;
              var filename = url.split("/").pop();
              alert(filename);
              var targetPath = cordova.file.externalRootDirectory + filename;
              var trustHosts = true
              var options = {};
              // alert(cordova.file.externalRootDirectory);
              $cordovaFileTransfer.download(url, targetPath, options, trustHosts)
                  .then(function(result) {
                      // Success!
                      alert(JSON.stringify(result));
                  }, function(error) {
                      // Error
                      alert(JSON.stringify(error));
                  }, function (progress) {
                      $timeout(function () {
                          $scope.downloadProgress = (progress.loaded / progress.total) * 100;
                      })
                  });
          }
}

3.view.js 执行方法

    <ion-item>
     <button class="button button-block button-calm" ng-click="downloadFile()"> 下载图片 </button>
    </ion-item>

注意事项:

目录对应的android 真实目录。
console.log(cordova.file.externalApplicationStorageDirectory); //file:///storage/emulated/0/Android/data/com.bntake.driver.in/
console.log(cordova.file.dataDirectory); //file:///data/user/0/com.bntake.driver.in/files/
console.log(cordova.file.externalDataDirectory); //file:///storage/emulated/0/Android/data/com.bntake.driver.in/files/
console.log(cordova.file.externalRootDirectory);//file:///storage/emulated/0/
console.log(cordova.file.externalCacheDirectory); //file:///storage/emulated/0/Android/data/com.bntake.driver.in/cache/
console.log(cordova.file.applicationStorageDirectory); //file:///data/user/0/com.bntake.driver.in/
console.log(cordova.file.cacheDirectory); //file:///data/user/0/com.bntake.driver.in/cache/
console.log(cordova.file);  //object

cordova.file.externalRootDirectory EACCES (Permission denied)"无法打开文件夹的权限问题。

修改  应用的文件夹读取权限。  在设置 ->应用->
某应用->权限->存储空间。(安卓系统的类似的功能,因为手机系统都做了适配)

请求超时的处理,和文件管理

$timeout,$cordovaFileTransfer

js 在index.html 的引入。与该项目无关。只有引入js文件,才可以在controller中引入model方法。

  <script src="js/services.js"></script>
  <script src="js/service_image.js"></script>

第一次创建项目,index.html插件的引入: 需要安装

  <script src="lib/ngCordova/dist/ng-cordova.js"></script>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 单例模式 适用场景:可能会在场景中使用到对象,但只有一个实例,加载时并不主动创建,需要时才创建 最常见的单例模式,...
    Obeing阅读 6,547评论 1 10
  • 通过AngularJS仿豆瓣一刻的案例:https://github.com/zhongxiaolian/doub...
    中小恋阅读 5,759评论 1 21
  • AngularJS是什么?AngularJs(后面就简称ng了)是一个用于设计动态web应用的结构框架。首先,它是...
    200813阅读 5,581评论 0 3
  • 《ijs》速成开发手册3.0 官方用户交流:iApp开发交流(1) 239547050iApp开发交流(2) 10...
    叶染柒丶阅读 10,833评论 0 7
  • 大文件的上传是我一直以来想学习的一个技术点,今天在项目闲暇之时,终于有机会自己尝试了一把,本文仅仅是个Demo,各...
    小久代码搬运阅读 6,128评论 0 1

友情链接更多精彩内容