下载// 或者封装function download(href, title) { ..._js download">
赞
踩
当后端返回的资源是链接时,此时可以使用 a
标签或者 window.location.href
直接打开。
1、a 标签形式
在H5中,a
标签新增了download
属性,包含该属性的链接被点击时,浏览器会以下载文件方式下载 href 属性上的链接。
- <a href="https://example.com" download="example.html">下载</a>
-
- // 或者封装
- function download(href, title) {
- const a = document.createElement('a');
- a.setAttribute('href', href);
- a.setAttribute('download', title);
- a.click();
- }
- download('https://example.com', 'test')
2、window.location.href 直接打开
window.location.href === 'https://example.com'
当后端返回的文件是流文件时,以 umi-request
请求方法为例
首先应在请求中设置返回类型:responseType: "blob"
- import request from "umi-request";
-
- export const
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。