当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 如何使用post请求下载文件

如何使用post请求下载文件

2019年02月27日  | 移动技术网IT编程  | 我要评论
使用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); }

 

 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网