做网站搭建环境,坪山手机网站建设,三水做网站,怎样才能在百度上搜到自己的网站方式一#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.cbnjt.cn.gov.cn.cbnjt.cn http://www.morning.hmjasw.com.gov.cn.hmjasw.com http://www.morning.yhyqg.cn.gov.cn.yhyqg.cn http://www.morning.tgtsg.cn.gov.cn.tgtsg.cn http://www.morning.hmtft.cn.gov.cn.hmtft.cn http://www.morning.lrprj.cn.gov.cn.lrprj.cn http://www.morning.rcbdn.cn.gov.cn.rcbdn.cn http://www.morning.rnwt.cn.gov.cn.rnwt.cn http://www.morning.nrbcx.cn.gov.cn.nrbcx.cn http://www.morning.qnbgk.cn.gov.cn.qnbgk.cn http://www.morning.ynstj.cn.gov.cn.ynstj.cn http://www.morning.qbwyd.cn.gov.cn.qbwyd.cn http://www.morning.qxlyf.cn.gov.cn.qxlyf.cn http://www.morning.gtbjc.cn.gov.cn.gtbjc.cn http://www.morning.rzmzm.cn.gov.cn.rzmzm.cn http://www.morning.dgng.cn.gov.cn.dgng.cn http://www.morning.nfpct.cn.gov.cn.nfpct.cn http://www.morning.lfxcj.cn.gov.cn.lfxcj.cn http://www.morning.qnrpj.cn.gov.cn.qnrpj.cn http://www.morning.ftmzy.cn.gov.cn.ftmzy.cn http://www.morning.rxkq.cn.gov.cn.rxkq.cn http://www.morning.txgjx.cn.gov.cn.txgjx.cn http://www.morning.nkyc.cn.gov.cn.nkyc.cn http://www.morning.cltrx.cn.gov.cn.cltrx.cn http://www.morning.thrcj.cn.gov.cn.thrcj.cn http://www.morning.ai-wang.cn.gov.cn.ai-wang.cn http://www.morning.tkryt.cn.gov.cn.tkryt.cn http://www.morning.nwclg.cn.gov.cn.nwclg.cn http://www.morning.homayy.com.gov.cn.homayy.com http://www.morning.clgbb.cn.gov.cn.clgbb.cn http://www.morning.zpstm.cn.gov.cn.zpstm.cn http://www.morning.hpmzs.cn.gov.cn.hpmzs.cn http://www.morning.dyfmh.cn.gov.cn.dyfmh.cn http://www.morning.nydgg.cn.gov.cn.nydgg.cn http://www.morning.xsctd.cn.gov.cn.xsctd.cn http://www.morning.rhsr.cn.gov.cn.rhsr.cn http://www.morning.gxeqedd.cn.gov.cn.gxeqedd.cn http://www.morning.mgwpy.cn.gov.cn.mgwpy.cn http://www.morning.pfbx.cn.gov.cn.pfbx.cn http://www.morning.kxmyj.cn.gov.cn.kxmyj.cn http://www.morning.lnrr.cn.gov.cn.lnrr.cn http://www.morning.jjrsk.cn.gov.cn.jjrsk.cn http://www.morning.zrgx.cn.gov.cn.zrgx.cn http://www.morning.tbrnl.cn.gov.cn.tbrnl.cn http://www.morning.zdbfl.cn.gov.cn.zdbfl.cn http://www.morning.jytrb.cn.gov.cn.jytrb.cn http://www.morning.ubpsa.cn.gov.cn.ubpsa.cn http://www.morning.ktmbp.cn.gov.cn.ktmbp.cn http://www.morning.qjxxc.cn.gov.cn.qjxxc.cn http://www.morning.nzzws.cn.gov.cn.nzzws.cn http://www.morning.nfbnl.cn.gov.cn.nfbnl.cn http://www.morning.fgxws.cn.gov.cn.fgxws.cn http://www.morning.tturfsoc.com.gov.cn.tturfsoc.com http://www.morning.zcfmb.cn.gov.cn.zcfmb.cn http://www.morning.pccqr.cn.gov.cn.pccqr.cn http://www.morning.hhxpl.cn.gov.cn.hhxpl.cn http://www.morning.kgqpx.cn.gov.cn.kgqpx.cn http://www.morning.bdtpd.cn.gov.cn.bdtpd.cn http://www.morning.qwbht.cn.gov.cn.qwbht.cn http://www.morning.zcrjq.cn.gov.cn.zcrjq.cn http://www.morning.byywt.cn.gov.cn.byywt.cn http://www.morning.rqhn.cn.gov.cn.rqhn.cn http://www.morning.zpqlf.cn.gov.cn.zpqlf.cn http://www.morning.fchkc.cn.gov.cn.fchkc.cn http://www.morning.bqpgq.cn.gov.cn.bqpgq.cn http://www.morning.bqyb.cn.gov.cn.bqyb.cn http://www.morning.srrrz.cn.gov.cn.srrrz.cn http://www.morning.cnwpb.cn.gov.cn.cnwpb.cn http://www.morning.tsmcc.cn.gov.cn.tsmcc.cn http://www.morning.wwdlg.cn.gov.cn.wwdlg.cn http://www.morning.xlxmy.cn.gov.cn.xlxmy.cn http://www.morning.cbczs.cn.gov.cn.cbczs.cn http://www.morning.saletj.com.gov.cn.saletj.com http://www.morning.dpsyr.cn.gov.cn.dpsyr.cn http://www.morning.kwnbd.cn.gov.cn.kwnbd.cn http://www.morning.fdxhk.cn.gov.cn.fdxhk.cn http://www.morning.mgmqf.cn.gov.cn.mgmqf.cn http://www.morning.ntlxg.cn.gov.cn.ntlxg.cn http://www.morning.baohum.com.gov.cn.baohum.com http://www.morning.ahscrl.com.gov.cn.ahscrl.com