export function downloadAsDocExtranet (url, params, fileName) {
const downloadInfoMsg = vueMain.$message({
message: "<div style='color: rgb(36,159,230); width: 600px; word-wrap: break-word'>正在下载文档:" + fileName + ",请您耐心等待......</div>",
type: "info",
duration: 0,
dangerouslyUseHTMLString: true
});
getAsBinary(url, params).then(res => {
downloadInfoMsg.close();
if (res && res.status === 200 && res.data.size > 0) {
var blob = new Blob([res.data]);
if (window.navigator && window.navigator.msSaveBlob) {
window.navigator.msSaveBlob(blob, fileName);
} else {
var href = window.URL.createObjectURL(blob);
var aElement = document.createElement("a");
aElement.href = href;
aElement.target = "_blank";
aElement.download = fileName;
document.body.appendChild(aElement);
aElement.click();
document.body.removeChild(aElement);
window.URL.revokeObjectURL(href);
}
} else {
vueMain.$message({ message: "文档(" + fileName + ")获取下载链接失败,请联系管理员!", type: "error", duration: 3000 });
}
}).catch((err) => {
downloadInfoMsg.close();
vueMain.$message({ message: "文档(" + fileName + ")获取下载链接失败,请联系管理员!", type: "error", duration: 3000 });
console.log(err);
});
}
/**
* 访问后端接口
* @param url url
* @param params 请求参数
* @returns {Promise<unknown>} 响应二进制流
*/
function getAsBinary (url, params) {
return new Promise((resolve, rejects) => {
request
.post(url, qs.stringify(params), { responseType: "blob" })
.then(res => {
resolve(res);
}).catch(err => {
rejects(err);
});
});
}