Content-Disposition

https://enable-cors.org/index.html

注意事項(xiàng):
1.當(dāng)代碼里面使用Content-Disposition來(lái)確保瀏覽器彈出下載對(duì)話框的時(shí)候。 response.addHeader("Content-Disposition","attachment");一定要確保沒(méi)有做過(guò)關(guān)于禁止瀏覽器緩存的操作。
代碼如下:

response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "No-cache");
response.setDateHeader("Expires", 0);

不然會(huì)發(fā)現(xiàn)下載功能在opera和firefox里面好好的沒(méi)問(wèn)題,在IE下面就是不行。

Response Header:

Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Authorization,DNT,User-Agent,Keep-Alive,Content-Type,accept,origin,X-Requested-With
Access-Control-Allow-Methods: GET,OPTIONS,PUT,DELETE
Access-Control-Allow-Origin: https://wkylin.com.cn
Access-Control-Expose-Headers: Content-Disposition
Connection: keep-alive
Content-Disposition: attachment;filename='wkylin.pdf'
Content-Security-Policy: frame-ancestors *
Content-Type: application/octet-stream; charset=utf-8
Date: Sun, 01 Sep 2019 06:34:20 GMT
Server: nginx/1.10.3 (Ubuntu)
Transfer-Encoding: chunked
Vary: Cookie
X-Frame-Options: SAMEORIGIN, allow-from

下載文件,文件名由后端返回....

api.downloadFiles(params).then(res => {
  if (res.data.type.indexOf('json') !== -1) {
    message.error('未知錯(cuò)誤,請(qǐng)稍候再試!');
  } else {
    const disposition = res.headers['content-disposition'];
    if (disposition) {
      const fileName = decodeURI(disposition.split('=')[1].replace(/\'/g, ''));
      const blob = new Blob([res.data]);
      const downloadElement = document.createElement('a');
      const href = URL.createObjectURL(blob); // 創(chuàng)建下載的鏈接
      downloadElement.href = href;
      downloadElement.download = fileName; // 下載后文件名
      document.body.appendChild(downloadElement); // 追加a標(biāo)簽
      downloadElement.click(); // 點(diǎn)擊下載
      document.body.removeChild(downloadElement); // 下載完成移除元素
      URL.revokeObjectURL(href); // 釋放掉blob對(duì)象
    } else {
      message.error('未知錯(cuò)誤,請(qǐng)稍候再試!');
    }
  }
})
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容