在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/C  HTML/ js 下載文件

js 下載文件

后臺(tái)傳文件流給我,我要怎么下載 post獲取的文件流是不是不能通過(guò)window.location.href下載?如果能下載該怎么處理,這種情況下前端location.href好像不能下載。如果是get的話(huà)是不是location.href就能下載?

回答
編輯回答
玩控

兩種方法(第二種貌似是后端加)
1.<a href="你的下載地址" download> 核心是download屬性。
2.下載地址加header("Content-Disposition:attachment;filename=xxx"); 核心是header。

2017年5月2日 00:22
編輯回答
愛(ài)是癌

后來(lái)解決了,但是不知道為什么文件名是797bb297-60ea-4069-911b-04133ce2b313這種類(lèi)型
后臺(tái)的人是設(shè)置了Content-Disposition,前端是是使用了vue和vueResource,vueResource在請(qǐng)求的時(shí)候需要加一句Vue.http.options.responseType = 'arraybuffer',然后再對(duì)返回的數(shù)據(jù)流做如下處理

downloadFile(content) {
      let blob = new Blob([content], { type: 'application/vnd.ms-excel' }) //根據(jù)實(shí)際情況設(shè)置type
      let objUrl = URL.createObjectURL(blob)
      window.location.href = objUrl
    }
2017年12月11日 02:03
編輯回答
吃藕丑

我們項(xiàng)目中使用的。

export const downloadFile = (fileName, url) => {
  if (isIE()) {
    ieDown(url)
  } else {
    const aLink = document.createElement('a');
    const evt = document.createEvent('MouseEvents');
    // var evt = document.createEvent("HTMLEvents")
    evt.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    aLink.download = fileName;
    aLink.href = url;
    aLink.dispatchEvent(evt)
  }
};

const ieDown = url => {
  window.open(url)
};

const isIE = () => {
  const explorer = window.navigator.userAgent;
  return explorer.indexOf('MSIE') >= 0 || explorer.indexOf('Trident/7.0') >= 0 || explorer.indexOf('Edge') >= 0;
};
2018年1月10日 03:28