继上一篇文章微信公众号内下载pdf等文件,受微信所限制,安卓和IOS不同处理方式 后觉得还有更好的解决办法,这次真的找到更加优化版本,一定需要后台配合才行:后台接口返回Blob,后端设置response.setHeader(“Content-Disposition”, “attachment;fileName=XXXX.pdf”);不用判断安卓还是IOS,统一前端点击按钮使用form表单提交,安卓会自动弹出默认浏览器引导,IOS会弹出下载窗口。这个应该是最优版本。
export const downloadform = (url, token) => {
var form = document.createElement("form");
var input = document.createElement("input");
input.name = "token";
input.value = token;
form.action = url;
form.method = "get";
form.style.display = "none";
document.body.appendChild(form);
form.appendChild(input);
form.submit();
document.body.removeChild(form);
};
这里有个知识点,form表单提交,method=‘get’请求 action 无法携带?后的参数。 method=“post’可以携带。
如果必须是get请求,解决办法:
var input = document.createElement("input");
var input2 = document.createElement("input");
input.name = "token";
input.value = XXX;
最后会成为url?token=XXX