如何使用post請求下載檔案

王小冉子君發表於2019-02-26
使用get請求下載檔案非常簡便,但是get請求的url有長度和大小的限制,所以當請求引數非常多時無法滿足需求,所以改成post請求
const res = await fetch(`xxxxxxxxx`, { method: `post`, body: JSON.stringify(params), credentials: `include`, headers: { `Cache-Control`: `max-age=0`, `Pragma`: `no-cache`, `Content-Type`: `application/json;charset=UTF-8`, `x-requested-with`: `fetch` } }); const blob = await res.blob(); if (`download` in document.createElement(`a`)) { var a = document.createElement(`a`); a.style.display = `none`; var url = window.URL.createObjectURL(blob); var filename = decodeURIComponent(res.headers.get(`Content-Disposition`)); a.href = url; a.download = filename; document.body.appendChild(a); a.click(); window.URL.revokeObjectURL(url); document.body.removeChild(a); } else { navigator.msSaveBlob(blob); }

 

 

 

相關文章