下载

qh.download()

下载文件,调起浏览器下载弹窗。(版本1.4.0开始支持)

  1. qh.download(opts);

参数值:

属性类型是否必填描述
urlstring下载资源的 url
fileNamestring指定文件保存的文件名,默认为资源的默认文件名
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用完成的回调函数(接口成功、失败都会执行)

success 回调函数:

成功调起下载窗口后,触发回调,无回调参数

qh.downloadFile()

下载文件资源到本地,客户端直接发起一个 HTTP GET 请求,返回文件的本地临时路径。

  1. qh.downloadFile(opts);

参数值:

属性类型是否必填描述
urlstring下载资源的 url
headerObjectHTTP 请求的 Header,Header 中不能设置 Referer
filePathstring指定文件下载后存储的路径
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用完成的回调函数(接口成功、失败都会执行)

返回值:

Object 类型的对象:

属性类型描述
DownloadTaskObjectDownloadTask 实例对象

success 回调函数:

形如 function (res) {…},其中:

属性类型描述
res.tempFilePathstring临时文件路径。如果没传入 filePath 指定文件存储路径,则下载后的文件会存储到一个临时文件
res.statusCodenumber开发者服务器返回的 HTTP 状态码

DownloadTask

一个可以监听下载进度变化事件,以及取消下载任务的对象。

DownloadTask.abort

中断下载任务。

DownloadTask.onProgressUpdate

监听下载进度变化事件。

  1. DownloadTask.onProgressUpdate(callback);

DownloadTask.offProgressUpdate

取消监听下载进度变化事件。

  1. DownloadTask.offProgressUpdate();

示例:

  1. const downloadUrl =
  2. "https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
  3. const path = "test.pdf";
  4. const task = qh.downloadFile({
  5. header: {
  6. "Cache-Control": "no-cache"
  7. },
  8. filePath: path,
  9. url: downloadUrl,
  10. success: res => console.log("downloadFile数据:", JSON.stringify(res))
  11. });
  12. task.onProgressUpdate(
  13. ({ progress, totalBytesWritten, totalBytesExpectedToWrite }) => {
  14. task.abort();
  15. console.log(
  16. `downloadFile progress:${progress}%, ${totalBytesWritten}/${totalBytesExpectedToWrite}`
  17. );
  18. }
  19. );