/**
* 文件下载
* @param fileUrl 文件url
* @param fileName 下载文件显示名称
*/
function downLoadFile(fileUrl, fileName){
var index = location.href.indexOf("index.html");
var pathName = location.href.substring(0, index);
var url = pathName + "rest/download/downloadfileByName?imageUrl="+fileUrl+"&filename="+fileName;
//调用后台接口,下载文件
window.open(url, "_self");
};
/**
* 文件下载
* @param id 文件id
* @param fileName 下载文件显示名称
*/
function downLoadFileById(id, fileName){
var index = location.href.indexOf("index.html");
var pathName = location.href.substring(0, index);
var url = pathName + "rest/download/downloadfileByName?id="+id+"&filename="+fileName;
//调用后台接口,下载文件
// window.open(url, "_self");
// 方法二:
$.ajax({
headers: {
"user_guid":localStorage.getItem('user_guid'),
"nonce":localStorage.getItem('nonce'),
}, // 上传请求头
type: "GET",
url: pathName + "rest/download/downloadfileByName",
data:{ id: id, filename: fileName },
dataType: 'binary',
xhrFields: { responseType: "blob" },
success: function (res) {
// res 为二进制流
console.log("成功--->>>>",res)
var blob = new Blob([res],{ type:'application/octet-stream'})
var url = window.URL.createObjectURL(blob)
var link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', fileName || 'download')
document.body.appendChild(link)
link.click()
link.remove()
},
complete: function (XMLHttpRequest,status) {
console.log('complete---->>>',XMLHttpRequest,status)
},
error: function (err) {
console.log('失败回调---->>>',err);
}
});
jquery下载文件
最新推荐文章于 2024-11-04 16:22:37 发布