下载图片

parent a2a4958a
......@@ -145,30 +145,6 @@ const convertSize = (sizeInKB) => {
// 下载图片
const handleDownload = () => {
// uni.downloadFile({
// url: previewImages.value[index.value],
// success: (res) => {
// if (res.statusCode === 200) {
// // 保存图片到相册
// plus.gallery.save(res.tempFilePath, () => {
// uni.showToast({
// title: '图片保存成功',
// icon: 'success'
// });
// }, (err) => {
// console.error('保存图片失败', err);
// uni.showToast({
// title: '图片保存失败',
// icon: 'none'
// });
// });
// } else {
// console.error('图片下载失败');
// }
// }, fail: (err) => {
// console.error('图片下载失败', err);
// }
// });
xma.downloadFile({
url: previewImages.value[index.value], // 替换为你要下载的图片URL
success: (res) => {
......@@ -185,46 +161,50 @@ const handleDownload = () => {
});
}
// function saveImage(filePath) {
// // 创建一个<a>元素
// const link = document.createElement('a');
// link.href = filePath;
// // 设置下载的文件名
// link.download = catalog.value.enclosureList[index.value].enclosureDesc + '.jpg';
// // 触发点击事件
// document.body.appendChild(link); link.click();
// // 移除该元素
// document.body.removeChild(link);
// }
function saveImage(url) {
fetch(url)
.then(response => response.blob())
.then(blob => {
// 创建一个下载链接
const link = document.createElement('a');
const objectUrl = URL.createObjectURL(blob);
link.href = objectUrl;
// 设置下载的文件名和后缀
link.download = catalog.value.enclosureList[index.value].enclosureDesc + '.jpg';
// 触发点击事件
document.body.appendChild(link);
link.click();
// 移除下载链接
document.body.removeChild(link);
URL.revokeObjectURL(objectUrl);
uni.showToast({
title: '下载成功',
icon: 'success'
})
})
.catch(error => {
uni.showToast({
title: '下载失败',
icon: 'none'
})
console.log('下载图片失败', error)
});
function saveImage(filePath) {
// 创建一个<a>元素
const link = document.createElement('a');
link.href = filePath;
// 设置下载的文件名
link.download = catalog.value.enclosureList[index.value].enclosureDesc + '.jpg';
// 触发点击事件
document.body.appendChild(link); link.click();
// 移除该元素
document.body.removeChild(link);
uni.showToast({
title: '下载成功',
icon: 'success'
})
}
// function saveImage(url) {
// fetch(url)
// .then(response => response.blob())
// .then(blob => {
// // 创建一个下载链接
// const link = document.createElement('a');
// const objectUrl = URL.createObjectURL(blob);
// link.href = objectUrl;
// // 设置下载的文件名和后缀
// link.download = catalog.value.enclosureList[index.value].enclosureDesc + '.jpg';
// // 触发点击事件
// document.body.appendChild(link);
// link.click();
// // 移除下载链接
// document.body.removeChild(link);
// URL.revokeObjectURL(objectUrl);
// uni.showToast({
// title: '下载成功',
// icon: 'success'
// })
// })
// .catch(error => {
// uni.showToast({
// title: '下载失败',
// icon: 'none'
// })
// console.log('下载图片失败', error)
// });
// }
</script>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment