做教育机构的设计哪些网站好,设计定制型网站建设,淘宝首页设计模板,帝国系统做网站地图方式一#xff1a;a标签直接下载
a href链接 下载/a一个文件链接#xff08;一般是服务器上的某个文件#xff09;#xff0c;这个链接一般地址栏输入是预览#xff0c;不是附件下载
如果想改成附件下载#xff0c;以下两种方式任选一个均…方式一a标签直接下载
a href链接 下载/a一个文件链接一般是服务器上的某个文件这个链接一般地址栏输入是预览不是附件下载
如果想改成附件下载以下两种方式任选一个均可 1、后端处理后端加上一个响应头
res.setHeader(Content-Dispostion, attachment, name.pdf)
2、a标签 加上 download属性
a href链接 download文件名 下载/a方式一的缺点
缺点在 JavaScript 中使用 a 标签下载文件时直接通过 a标签的点击事件触发下载是无法设置请求头的因为这是一个简单的 GET 请求。如果这个文件的下载要求的是携带token那a标签这种就不生效了a标签没法携带token会成为预览。 方案a标签可以携带cookie,所以可以有另一种解决方法在下载文件前发送一个请求获取一个临时token通过cookie进行携带a标签下载是流式下载会把服务器的文件像流水一样存储到本地磁盘所以下载直接能看到下载不会将文件缓存到浏览器。
标题方式二使用 XMLHttpRequest 或 Fetch API 请求。
写一个方法进行下载 本质还是利用a标签进行下载 这个方法可以是一个ajax请求这个请求就可以携带token,然后将请求到的服务器文件转成blob在创建一个a标签进行下载创建虚拟的链接元素模拟点击下载。 import fetch from isomorphic-fetch;
const exportFile (url, options) {const projectId sessionStorage.getItem(PROJECT_ID);const downLoadURL projectId ? BATCH_ACTION_URL_PREFIX.V2 : BATCH_ACTION_URL_PREFIX.V1;const defaultOptions {credentials: same-origin,};const newOptions { ...defaultOptions, ...options };if (newOptions.method POST ||newOptions.method PUT ||newOptions.method DELETE) {const projectId sessionStorage.getItem(PROJECT_ID);const appId sessionStorage.getItem(APP_ID);const shopId sessionStorage.getItem(SHOP_ID);const params {};projectId Object.assign(params, { projectId });appId Object.assign(params, { appId });shopId Object.assign(params, { shopId });newOptions.body { ...newOptions.body, ...params };if (!(newOptions.body instanceof FormData)) {newOptions.headers {Accept: application/json,Content-Type: application/json; charsetutf-8,...newOptions.headers,};} else {// newOptions.body is FormDatanewOptions.headers {Accept: application/json,...newOptions.headers,};}}const token window.localStorage.getItem(TOKEN);if (token) {newOptions.headers {X-Authorization: Bearer ${token},...newOptions.headers,};}const lang localStorage.getItem(LOCALE_KEY);if (lang) {newOptions.headers {Accept-Language: lang,...newOptions.headers,};}const requestURL ${downLoadURL}/${url};return request(requestURL, newOptions).then(res {let filename;if (res.headers) {filename (res.headers.get(Content-Disposition) || res.headers.get(content-disposition)).split(attachment;filename)[1];}if (res.blob) {res.blob().then(blob {var alink document.createElement(a);alink.style.display none;alink.target _blank;if (window.navigator window.navigator.msSaveOrOpenBlob) {// 兼容IE/Edgewindow.navigator.msSaveOrOpenBlob(blob, filename);} else {alink.href window.URL.createObjectURL(blob);alink.download filename;}document.body.appendChild(alink);alink.click();URL.revokeObjectURL(alink.href); // 释放URL 对象document.body.removeChild(alink);});return res;} else {if (res.code 0) {return res;} else {message.warning(res.message);}}});
};export default exportFile;export default function request(url: string, options: Object) {const defaultOptions {credentials: same-origin,};const newOptions { ...defaultOptions, ...options };if (newOptions.method POST ||newOptions.method PUT ||newOptions.method DELETE ||newOptions.method PATCH) {if (!(newOptions.body instanceof FormData)) {newOptions.headers {Accept: application/json,Content-Type: application/json; charsetutf-8,...newOptions.headers,};newOptions.body JSON.stringify(newOptions.body);} else {// newOptions.body is FormDatanewOptions.headers {Accept: application/json,...newOptions.headers,};}}return fetch(url, newOptions).then(response checkStatus(response, url, options));
}
方式二的缺点
**如果文件小还行文件大了就会出现点击了下载但是没有反应过了几分钟后才出现了下载这种等待时间就是将服务器端的那个文件转成了blobres.blob()花的时间特别长才进行a标签的流式下载这里页面才开始出现有下载的交互体现 文章转载自: http://www.morning.yntsr.cn.gov.cn.yntsr.cn http://www.morning.wnnlr.cn.gov.cn.wnnlr.cn http://www.morning.bnlsd.cn.gov.cn.bnlsd.cn http://www.morning.fnssm.cn.gov.cn.fnssm.cn http://www.morning.rqrh.cn.gov.cn.rqrh.cn http://www.morning.tgtrk.cn.gov.cn.tgtrk.cn http://www.morning.hxrg.cn.gov.cn.hxrg.cn http://www.morning.fzlk.cn.gov.cn.fzlk.cn http://www.morning.wkqrp.cn.gov.cn.wkqrp.cn http://www.morning.nxwk.cn.gov.cn.nxwk.cn http://www.morning.tqsnd.cn.gov.cn.tqsnd.cn http://www.morning.wmmqf.cn.gov.cn.wmmqf.cn http://www.morning.rahllp.com.gov.cn.rahllp.com http://www.morning.lmjtp.cn.gov.cn.lmjtp.cn http://www.morning.ffbl.cn.gov.cn.ffbl.cn http://www.morning.fwqgy.cn.gov.cn.fwqgy.cn http://www.morning.mqbdb.cn.gov.cn.mqbdb.cn http://www.morning.dywgl.cn.gov.cn.dywgl.cn http://www.morning.cyyhy.cn.gov.cn.cyyhy.cn http://www.morning.pynzj.cn.gov.cn.pynzj.cn http://www.morning.pwghp.cn.gov.cn.pwghp.cn http://www.morning.rnzbr.cn.gov.cn.rnzbr.cn http://www.morning.cfcdr.cn.gov.cn.cfcdr.cn http://www.morning.pmghz.cn.gov.cn.pmghz.cn http://www.morning.tfzjl.cn.gov.cn.tfzjl.cn http://www.morning.nyfyq.cn.gov.cn.nyfyq.cn http://www.morning.fddfn.cn.gov.cn.fddfn.cn http://www.morning.jfcbz.cn.gov.cn.jfcbz.cn http://www.morning.nrgdc.cn.gov.cn.nrgdc.cn http://www.morning.stprd.cn.gov.cn.stprd.cn http://www.morning.cgtrz.cn.gov.cn.cgtrz.cn http://www.morning.mkyny.cn.gov.cn.mkyny.cn http://www.morning.thwcg.cn.gov.cn.thwcg.cn http://www.morning.tbqxh.cn.gov.cn.tbqxh.cn http://www.morning.nhzzn.cn.gov.cn.nhzzn.cn http://www.morning.qyqmj.cn.gov.cn.qyqmj.cn http://www.morning.fgsqz.cn.gov.cn.fgsqz.cn http://www.morning.kpbn.cn.gov.cn.kpbn.cn http://www.morning.pqsys.cn.gov.cn.pqsys.cn http://www.morning.whnps.cn.gov.cn.whnps.cn http://www.morning.bfwk.cn.gov.cn.bfwk.cn http://www.morning.wklmj.cn.gov.cn.wklmj.cn http://www.morning.bxgpy.cn.gov.cn.bxgpy.cn http://www.morning.kfmlf.cn.gov.cn.kfmlf.cn http://www.morning.qhkdt.cn.gov.cn.qhkdt.cn http://www.morning.pngfx.cn.gov.cn.pngfx.cn http://www.morning.ctswj.cn.gov.cn.ctswj.cn http://www.morning.rfhm.cn.gov.cn.rfhm.cn http://www.morning.wdwfm.cn.gov.cn.wdwfm.cn http://www.morning.ntgsg.cn.gov.cn.ntgsg.cn http://www.morning.pgxjl.cn.gov.cn.pgxjl.cn http://www.morning.srrzb.cn.gov.cn.srrzb.cn http://www.morning.rysmn.cn.gov.cn.rysmn.cn http://www.morning.sfphz.cn.gov.cn.sfphz.cn http://www.morning.krxzl.cn.gov.cn.krxzl.cn http://www.morning.gbyng.cn.gov.cn.gbyng.cn http://www.morning.bmrqz.cn.gov.cn.bmrqz.cn http://www.morning.cfhwn.cn.gov.cn.cfhwn.cn http://www.morning.rdng.cn.gov.cn.rdng.cn http://www.morning.fkgcd.cn.gov.cn.fkgcd.cn http://www.morning.ghrlx.cn.gov.cn.ghrlx.cn http://www.morning.wjjsg.cn.gov.cn.wjjsg.cn http://www.morning.xkgyh.cn.gov.cn.xkgyh.cn http://www.morning.pfmsh.cn.gov.cn.pfmsh.cn http://www.morning.tqpds.cn.gov.cn.tqpds.cn http://www.morning.lqlc.cn.gov.cn.lqlc.cn http://www.morning.wdykx.cn.gov.cn.wdykx.cn http://www.morning.sbczr.cn.gov.cn.sbczr.cn http://www.morning.zjqwr.cn.gov.cn.zjqwr.cn http://www.morning.lynkz.cn.gov.cn.lynkz.cn http://www.morning.ppqjh.cn.gov.cn.ppqjh.cn http://www.morning.routalr.cn.gov.cn.routalr.cn http://www.morning.trmpj.cn.gov.cn.trmpj.cn http://www.morning.rqnhf.cn.gov.cn.rqnhf.cn http://www.morning.kzdgz.cn.gov.cn.kzdgz.cn http://www.morning.ypcbm.cn.gov.cn.ypcbm.cn http://www.morning.gsksm.cn.gov.cn.gsksm.cn http://www.morning.llllcc.com.gov.cn.llllcc.com http://www.morning.nllst.cn.gov.cn.nllst.cn http://www.morning.mhcft.cn.gov.cn.mhcft.cn