2025-04-02 18:20:17 +08:00
|
|
|
|
/**放大*/
|
|
|
|
|
|
function viewImg(item) {
|
|
|
|
|
|
layer.photos({
|
|
|
|
|
|
shade: 0.5,
|
|
|
|
|
|
footer: false,
|
|
|
|
|
|
photos: {
|
|
|
|
|
|
"title": "图片预览",
|
|
|
|
|
|
"start": 0,
|
|
|
|
|
|
"data": [
|
|
|
|
|
|
{
|
|
|
|
|
|
"pid": 1,
|
|
|
|
|
|
"src": imgUrl + item.originalFilePath + "?token=" + tokens,
|
|
|
|
|
|
}
|
|
|
|
|
|
]
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**原图下载*/
|
|
|
|
|
|
function imgDownLoad(item) {
|
|
|
|
|
|
let orginalPath = item.originalFilePath;
|
|
|
|
|
|
let obj = {
|
|
|
|
|
|
imgPath: orginalPath,
|
|
|
|
|
|
}
|
|
|
|
|
|
let loadingMsg = layer.msg("原图下载中,请稍候...", {icon: 16, scrollbar: false, time: 0,});
|
|
|
|
|
|
let url = dataUrl + "/common/download/downloadImage?token=" + tokens + "&encryptedData=" + encodeURIComponent(encryptCBC(JSON.stringify(obj)));
|
|
|
|
|
|
let xhr = new XMLHttpRequest();
|
|
|
|
|
|
xhr.open("get", url, true);
|
|
|
|
|
|
xhr.responseType = "blob"; // 转换流
|
2025-04-02 18:48:24 +08:00
|
|
|
|
xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8')
|
2025-04-02 18:20:17 +08:00
|
|
|
|
xhr.onload = function () {
|
|
|
|
|
|
layer.close(loadingMsg);
|
|
|
|
|
|
if (this.status === 200) {
|
|
|
|
|
|
let blob = this.response;
|
|
|
|
|
|
var a = document.createElement("a");
|
|
|
|
|
|
var url = window.URL.createObjectURL(blob);
|
|
|
|
|
|
a.href = url;
|
2025-04-02 18:48:24 +08:00
|
|
|
|
a.download = orginalPath.substring(orginalPath.lastIndexOf('/') + 1, orginalPath.length); // 文件名
|
2025-04-02 18:20:17 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
layer.msg("原图下载发生异常,请稍后重试", {icon: 16, scrollbar: false, time: 2000});
|
|
|
|
|
|
}
|
|
|
|
|
|
a.click();
|
|
|
|
|
|
window.URL.revokeObjectURL(url);
|
|
|
|
|
|
};
|
|
|
|
|
|
// xhr.send(params);
|
|
|
|
|
|
xhr.send();
|
2025-04-02 18:48:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**水印下载*/
|
|
|
|
|
|
function waterImgDownLoad(item) {
|
|
|
|
|
|
/* let orginalPath = item.originalFilePath;
|
|
|
|
|
|
let obj = {
|
|
|
|
|
|
imgPath: orginalPath,
|
|
|
|
|
|
}
|
|
|
|
|
|
let loadingMsg = layer.msg("水印图片下载中,请稍候...", {icon: 16, scrollbar: false, time: 0,});
|
|
|
|
|
|
let url = dataUrl + "/common/download/downloadImage?token=" + tokens + "&encryptedData=" + encodeURIComponent(encryptCBC(JSON.stringify(obj)));
|
|
|
|
|
|
let xhr = new XMLHttpRequest();
|
|
|
|
|
|
xhr.open("get", url, true);
|
|
|
|
|
|
xhr.responseType = "blob"; // 转换流
|
|
|
|
|
|
xhr.setRequestHeader('Content-Type','application/json;charset=UTF-8')
|
|
|
|
|
|
xhr.onload = function () {
|
|
|
|
|
|
layer.close(loadingMsg);
|
|
|
|
|
|
if (this.status === 200) {
|
|
|
|
|
|
let blob = this.response;
|
|
|
|
|
|
var a = document.createElement("a");
|
|
|
|
|
|
var url = window.URL.createObjectURL(blob);
|
|
|
|
|
|
a.href = url;
|
|
|
|
|
|
a.download = orginalPath.substring(orginalPath.lastIndexOf('/') + 1,orginalPath.length); // 文件名
|
|
|
|
|
|
} else {
|
|
|
|
|
|
layer.msg("水印图片下载发生异常,请稍后重试", {icon: 16, scrollbar: false, time: 2000});
|
|
|
|
|
|
}
|
|
|
|
|
|
a.click();
|
|
|
|
|
|
window.URL.revokeObjectURL(url);
|
|
|
|
|
|
};
|
|
|
|
|
|
// xhr.send(params);
|
|
|
|
|
|
xhr.send();*/
|
|
|
|
|
|
|
|
|
|
|
|
function downloadImage(imageUrl) {
|
|
|
|
|
|
let orginalPath = item.originalFilePath;
|
|
|
|
|
|
let obj = {
|
|
|
|
|
|
imgPath: orginalPath,
|
|
|
|
|
|
}
|
|
|
|
|
|
$.ajax({
|
|
|
|
|
|
url: dataUrl + "/common/download/downloadImage?token=" + tokens,
|
|
|
|
|
|
type: 'GET',
|
|
|
|
|
|
data: {
|
|
|
|
|
|
encryptedData: encodeURIComponent(encryptCBC(JSON.stringify(obj)))
|
|
|
|
|
|
},
|
|
|
|
|
|
xhrFields: {
|
|
|
|
|
|
responseType: 'blob' // 重要:指定响应类型为blob
|
|
|
|
|
|
},
|
|
|
|
|
|
success: function (data, status, xhr) {
|
|
|
|
|
|
// 检查是否是blob数据(图片)
|
|
|
|
|
|
if (data instanceof Blob) {
|
|
|
|
|
|
// 创建临时URL用于下载
|
|
|
|
|
|
var blobUrl = URL.createObjectURL(data);
|
|
|
|
|
|
|
|
|
|
|
|
// 创建下载链接
|
|
|
|
|
|
var a = document.createElement('a');
|
|
|
|
|
|
a.href = blobUrl;
|
|
|
|
|
|
a.download = orginalPath.substring(orginalPath.lastIndexOf('/') + 1,orginalPath.length); // 提取文件名
|
|
|
|
|
|
document.body.appendChild(a);
|
|
|
|
|
|
a.click();
|
|
|
|
|
|
|
|
|
|
|
|
// 清理
|
|
|
|
|
|
setTimeout(function () {
|
|
|
|
|
|
document.body.removeChild(a);
|
|
|
|
|
|
URL.revokeObjectURL(blobUrl);
|
|
|
|
|
|
}, 100);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 如果返回的不是blob,可能是错误信息
|
|
|
|
|
|
console.error('服务器返回意外响应:', data);
|
|
|
|
|
|
alert('下载失败: 服务器返回无效数据');
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
error: function (xhr, status, error) {
|
|
|
|
|
|
// 尝试解析错误信息
|
|
|
|
|
|
var errorMsg = '下载失败';
|
|
|
|
|
|
if (xhr.responseText) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
var errorResponse = JSON.parse(xhr.responseText);
|
|
|
|
|
|
errorMsg = errorResponse.message || errorMsg;
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
errorMsg = xhr.responseText;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
alert(errorMsg);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 使用示例
|
|
|
|
|
|
$('#download-btn').click(function () {
|
|
|
|
|
|
var imageUrl = $('#image-url').val();
|
|
|
|
|
|
downloadImage(imageUrl);
|
|
|
|
|
|
});
|
2025-04-02 18:20:17 +08:00
|
|
|
|
}
|