HBuilder打包app下载文件问题处理

HBuilder打包app下载文件问题处理:

  问题:刚开始使用<a>标签的download属性下载,无法支持IOS,只能在安卓app内下载。

  处理方式:使用HTML5+ api 中的Downloader处理

  Downloader模块管理网络文件下载任务,用于从服务器下载各种文件,并支持跨域访问操作。

通过plus.downloader获取下载管理对象。

  Downloader下载使用HTTP的GET/POST方式请求下载文件,符合标准HTTP/HTTPS传输协议。

downLoadClick(value) {
      let _this = this;
      _this.lodingShow = true;
      let dtask = null;
      if (~navigator.userAgent.indexOf("Html5Plus")) {
        let plusReady = function(callback) {
          if (window.plus) {
            callback();
          } else {
            document.addEventListener("plusready", callback);
          }
        };
        plusReady(function() {
          dtask = plus.downloader.createDownload(
            value.url,
            {
              method: "GET",
              filename: `_documents/download/${value.fileName}`
            },
            function(d, status) {
              _this.lodingShow = false;
              if (status == 200) {
                _this.alertVal = `<p>文件下载成功:</p>${d.filename}`;
                _this.showPluginAuto();
                plus.runtime.openFile(d.filename);  
              } else {
                _this.alertVal = "文件下载失败";
                _this.showPluginAuto();
              }
            }
          );
          dtask.start();
        });
      }
    }

  下载成功后:默认情况下,文件放在了Android/data/io.dcloud.hbuilder/downloads文件夹下面了

  iOS : 通常在设备应用沙盒目录下“/Library/Pandora/documents”

  plus.runtime.openFile(d.filename);  // 调用相应的默认的第三方程序打开文件

  注意:filename保存文件路径仅支持以"_downloads/"、"_doc/"、"_documents/"开头的字符串。

文件路径以文件后缀名结尾(如"_doc/download/a.doc")表明指定保存文件目录及名称,以“/”结尾则认为指定保存文件的目录(此时程序自动生成文件名)。

如果指定的文件已经存在,则自动在文件名后面加"(i)",其中i为数字,如果文件名称后面已经是此格式,则数字i递增,如"download(1).doc"。

默认保存目录为("_downloads"),并自动生成文件名称。

posted @ 2019-05-28 10:37  zigood  阅读(3003)  评论(0编辑  收藏  举报