diff --git a/src/utils/download.ts b/src/utils/download.ts new file mode 100644 index 0000000..b551651 --- /dev/null +++ b/src/utils/download.ts @@ -0,0 +1,49 @@ +/** + * 下载图片(本地地址) + * @param href 图片地址 + * @param name 图片名称 + */ +function downloadImg(href: string, name: string) { + console.log(href, 'href') + const eleLink = document.createElement('a') + eleLink.download = name + eleLink.href = href + eleLink.click() + eleLink.remove() +} + +/** + * 下载文件 + * @param url 文件地址 + * @param name 文件名 + */ +export function download(url: string, name: string) { + // 下载图片和文件 + const photo = ['jpg', 'jpeg', 'png'] + // const file = ['doc', 'excel', 'pdf'] + const fileType = url.split('.')[url.split('.').length - 1] + console.log(fileType, 'fileType') + if (photo.includes(fileType)) { + // 图片 将服务器图片地址转化成本地地址 + const image = new Image() + image.setAttribute('crossOrigin', 'anonymous') + image.src = url + image.onload = () => { + const canvas = document.createElement('canvas') + canvas.width = image.width + canvas.height = image.height + const ctx = canvas.getContext('2d'); + (ctx as CanvasRenderingContext2D).drawImage(image, 0, 0, image.width, image.height) + canvas.toBlob((blob) => { + const url = URL.createObjectURL(blob as Blob) + downloadImg(url, name) + // 用完释放URL对象 + URL.revokeObjectURL(url) + }) + } + } + else { + // 文件 + window.open(url) + } +} diff --git a/src/utils/download.ts b/src/utils/download.ts new file mode 100644 index 0000000..b551651 --- /dev/null +++ b/src/utils/download.ts @@ -0,0 +1,49 @@ +/** + * 下载图片(本地地址) + * @param href 图片地址 + * @param name 图片名称 + */ +function downloadImg(href: string, name: string) { + console.log(href, 'href') + const eleLink = document.createElement('a') + eleLink.download = name + eleLink.href = href + eleLink.click() + eleLink.remove() +} + +/** + * 下载文件 + * @param url 文件地址 + * @param name 文件名 + */ +export function download(url: string, name: string) { + // 下载图片和文件 + const photo = ['jpg', 'jpeg', 'png'] + // const file = ['doc', 'excel', 'pdf'] + const fileType = url.split('.')[url.split('.').length - 1] + console.log(fileType, 'fileType') + if (photo.includes(fileType)) { + // 图片 将服务器图片地址转化成本地地址 + const image = new Image() + image.setAttribute('crossOrigin', 'anonymous') + image.src = url + image.onload = () => { + const canvas = document.createElement('canvas') + canvas.width = image.width + canvas.height = image.height + const ctx = canvas.getContext('2d'); + (ctx as CanvasRenderingContext2D).drawImage(image, 0, 0, image.width, image.height) + canvas.toBlob((blob) => { + const url = URL.createObjectURL(blob as Blob) + downloadImg(url, name) + // 用完释放URL对象 + URL.revokeObjectURL(url) + }) + } + } + else { + // 文件 + window.open(url) + } +} diff --git a/src/views/system/tool/certificate/addDDialog.vue b/src/views/system/tool/certificate/addDDialog.vue index e56f293..d856aff 100644 --- a/src/views/system/tool/certificate/addDDialog.vue +++ b/src/views/system/tool/certificate/addDDialog.vue @@ -92,7 +92,7 @@ // }) } ruleForm.templateType = row.templateType - getuser() + // getuser() } defineExpose({ initDialog }) // 关闭弹窗 diff --git a/src/utils/download.ts b/src/utils/download.ts new file mode 100644 index 0000000..b551651 --- /dev/null +++ b/src/utils/download.ts @@ -0,0 +1,49 @@ +/** + * 下载图片(本地地址) + * @param href 图片地址 + * @param name 图片名称 + */ +function downloadImg(href: string, name: string) { + console.log(href, 'href') + const eleLink = document.createElement('a') + eleLink.download = name + eleLink.href = href + eleLink.click() + eleLink.remove() +} + +/** + * 下载文件 + * @param url 文件地址 + * @param name 文件名 + */ +export function download(url: string, name: string) { + // 下载图片和文件 + const photo = ['jpg', 'jpeg', 'png'] + // const file = ['doc', 'excel', 'pdf'] + const fileType = url.split('.')[url.split('.').length - 1] + console.log(fileType, 'fileType') + if (photo.includes(fileType)) { + // 图片 将服务器图片地址转化成本地地址 + const image = new Image() + image.setAttribute('crossOrigin', 'anonymous') + image.src = url + image.onload = () => { + const canvas = document.createElement('canvas') + canvas.width = image.width + canvas.height = image.height + const ctx = canvas.getContext('2d'); + (ctx as CanvasRenderingContext2D).drawImage(image, 0, 0, image.width, image.height) + canvas.toBlob((blob) => { + const url = URL.createObjectURL(blob as Blob) + downloadImg(url, name) + // 用完释放URL对象 + URL.revokeObjectURL(url) + }) + } + } + else { + // 文件 + window.open(url) + } +} diff --git a/src/views/system/tool/certificate/addDDialog.vue b/src/views/system/tool/certificate/addDDialog.vue index e56f293..d856aff 100644 --- a/src/views/system/tool/certificate/addDDialog.vue +++ b/src/views/system/tool/certificate/addDDialog.vue @@ -92,7 +92,7 @@ // }) } ruleForm.templateType = row.templateType - getuser() + // getuser() } defineExpose({ initDialog }) // 关闭弹窗 diff --git a/src/views/system/tool/certificate/certificate.vue b/src/views/system/tool/certificate/certificate.vue index 14b11aa..c4ad507 100644 --- a/src/views/system/tool/certificate/certificate.vue +++ b/src/views/system/tool/certificate/certificate.vue @@ -10,6 +10,7 @@ import { getDictByCode } from '@/api/system/dict' // import { exportExcel } from '@/utils/exportXlsx' import { exportFile } from '@/utils/exportUtils' +import { download } from '@/utils/download' const { proxy } = getCurrentInstance() as any const createTime = ref() const searchQuery = reactive({ @@ -141,7 +142,8 @@ if (row.minioFileName) { getPhotoUrl(row.minioFileName).then((res) => { fileUrl.value = res.data - fileUrl.value ? downRef.value.click() : ElMessage('无可下载内容') + // fileUrl.value ? downRef.value.click() : ElMessage('无可下载内容') + download(res.data, row.templateName as string) }).catch(() => { ElMessage.error('下载失败') }) diff --git a/src/utils/download.ts b/src/utils/download.ts new file mode 100644 index 0000000..b551651 --- /dev/null +++ b/src/utils/download.ts @@ -0,0 +1,49 @@ +/** + * 下载图片(本地地址) + * @param href 图片地址 + * @param name 图片名称 + */ +function downloadImg(href: string, name: string) { + console.log(href, 'href') + const eleLink = document.createElement('a') + eleLink.download = name + eleLink.href = href + eleLink.click() + eleLink.remove() +} + +/** + * 下载文件 + * @param url 文件地址 + * @param name 文件名 + */ +export function download(url: string, name: string) { + // 下载图片和文件 + const photo = ['jpg', 'jpeg', 'png'] + // const file = ['doc', 'excel', 'pdf'] + const fileType = url.split('.')[url.split('.').length - 1] + console.log(fileType, 'fileType') + if (photo.includes(fileType)) { + // 图片 将服务器图片地址转化成本地地址 + const image = new Image() + image.setAttribute('crossOrigin', 'anonymous') + image.src = url + image.onload = () => { + const canvas = document.createElement('canvas') + canvas.width = image.width + canvas.height = image.height + const ctx = canvas.getContext('2d'); + (ctx as CanvasRenderingContext2D).drawImage(image, 0, 0, image.width, image.height) + canvas.toBlob((blob) => { + const url = URL.createObjectURL(blob as Blob) + downloadImg(url, name) + // 用完释放URL对象 + URL.revokeObjectURL(url) + }) + } + } + else { + // 文件 + window.open(url) + } +} diff --git a/src/views/system/tool/certificate/addDDialog.vue b/src/views/system/tool/certificate/addDDialog.vue index e56f293..d856aff 100644 --- a/src/views/system/tool/certificate/addDDialog.vue +++ b/src/views/system/tool/certificate/addDDialog.vue @@ -92,7 +92,7 @@ // }) } ruleForm.templateType = row.templateType - getuser() + // getuser() } defineExpose({ initDialog }) // 关闭弹窗 diff --git a/src/views/system/tool/certificate/certificate.vue b/src/views/system/tool/certificate/certificate.vue index 14b11aa..c4ad507 100644 --- a/src/views/system/tool/certificate/certificate.vue +++ b/src/views/system/tool/certificate/certificate.vue @@ -10,6 +10,7 @@ import { getDictByCode } from '@/api/system/dict' // import { exportExcel } from '@/utils/exportXlsx' import { exportFile } from '@/utils/exportUtils' +import { download } from '@/utils/download' const { proxy } = getCurrentInstance() as any const createTime = ref() const searchQuery = reactive({ @@ -141,7 +142,8 @@ if (row.minioFileName) { getPhotoUrl(row.minioFileName).then((res) => { fileUrl.value = res.data - fileUrl.value ? downRef.value.click() : ElMessage('无可下载内容') + // fileUrl.value ? downRef.value.click() : ElMessage('无可下载内容') + download(res.data, row.templateName as string) }).catch(() => { ElMessage.error('下载失败') }) diff --git a/src/views/system/tool/document/addDDialog.vue b/src/views/system/tool/document/addDDialog.vue index cf7dec3..70840e2 100644 --- a/src/views/system/tool/document/addDDialog.vue +++ b/src/views/system/tool/document/addDDialog.vue @@ -92,7 +92,7 @@ // }) } ruleForm.templateType = row.templateType - getuser() + // getuser() } defineExpose({ initDialog }) // 关闭弹窗 diff --git a/src/utils/download.ts b/src/utils/download.ts new file mode 100644 index 0000000..b551651 --- /dev/null +++ b/src/utils/download.ts @@ -0,0 +1,49 @@ +/** + * 下载图片(本地地址) + * @param href 图片地址 + * @param name 图片名称 + */ +function downloadImg(href: string, name: string) { + console.log(href, 'href') + const eleLink = document.createElement('a') + eleLink.download = name + eleLink.href = href + eleLink.click() + eleLink.remove() +} + +/** + * 下载文件 + * @param url 文件地址 + * @param name 文件名 + */ +export function download(url: string, name: string) { + // 下载图片和文件 + const photo = ['jpg', 'jpeg', 'png'] + // const file = ['doc', 'excel', 'pdf'] + const fileType = url.split('.')[url.split('.').length - 1] + console.log(fileType, 'fileType') + if (photo.includes(fileType)) { + // 图片 将服务器图片地址转化成本地地址 + const image = new Image() + image.setAttribute('crossOrigin', 'anonymous') + image.src = url + image.onload = () => { + const canvas = document.createElement('canvas') + canvas.width = image.width + canvas.height = image.height + const ctx = canvas.getContext('2d'); + (ctx as CanvasRenderingContext2D).drawImage(image, 0, 0, image.width, image.height) + canvas.toBlob((blob) => { + const url = URL.createObjectURL(blob as Blob) + downloadImg(url, name) + // 用完释放URL对象 + URL.revokeObjectURL(url) + }) + } + } + else { + // 文件 + window.open(url) + } +} diff --git a/src/views/system/tool/certificate/addDDialog.vue b/src/views/system/tool/certificate/addDDialog.vue index e56f293..d856aff 100644 --- a/src/views/system/tool/certificate/addDDialog.vue +++ b/src/views/system/tool/certificate/addDDialog.vue @@ -92,7 +92,7 @@ // }) } ruleForm.templateType = row.templateType - getuser() + // getuser() } defineExpose({ initDialog }) // 关闭弹窗 diff --git a/src/views/system/tool/certificate/certificate.vue b/src/views/system/tool/certificate/certificate.vue index 14b11aa..c4ad507 100644 --- a/src/views/system/tool/certificate/certificate.vue +++ b/src/views/system/tool/certificate/certificate.vue @@ -10,6 +10,7 @@ import { getDictByCode } from '@/api/system/dict' // import { exportExcel } from '@/utils/exportXlsx' import { exportFile } from '@/utils/exportUtils' +import { download } from '@/utils/download' const { proxy } = getCurrentInstance() as any const createTime = ref() const searchQuery = reactive({ @@ -141,7 +142,8 @@ if (row.minioFileName) { getPhotoUrl(row.minioFileName).then((res) => { fileUrl.value = res.data - fileUrl.value ? downRef.value.click() : ElMessage('无可下载内容') + // fileUrl.value ? downRef.value.click() : ElMessage('无可下载内容') + download(res.data, row.templateName as string) }).catch(() => { ElMessage.error('下载失败') }) diff --git a/src/views/system/tool/document/addDDialog.vue b/src/views/system/tool/document/addDDialog.vue index cf7dec3..70840e2 100644 --- a/src/views/system/tool/document/addDDialog.vue +++ b/src/views/system/tool/document/addDDialog.vue @@ -92,7 +92,7 @@ // }) } ruleForm.templateType = row.templateType - getuser() + // getuser() } defineExpose({ initDialog }) // 关闭弹窗 diff --git a/src/views/system/tool/document/document.vue b/src/views/system/tool/document/document.vue index c5732f4..0113b9e 100644 --- a/src/views/system/tool/document/document.vue +++ b/src/views/system/tool/document/document.vue @@ -9,6 +9,7 @@ import { printJSON } from '@/utils/printUtils' import { getDictByCode } from '@/api/system/dict' import { exportFile } from '@/utils/exportUtils' +import { download } from '@/utils/download' const { proxy } = getCurrentInstance() as any const createTime = ref() const searchQuery = reactive({ @@ -138,8 +139,10 @@ const exportSele = (row: templateType) => { if (row.minioFileName) { getPhotoUrl(row.minioFileName).then((res) => { + console.log(row, res.data, 'res.datares.datares.data') fileUrl.value = res.data - fileUrl.value ? downRef.value.click() : ElMessage('无可下载内容') + // fileUrl.value ? downRef.value.click() : ElMessage('无可下载内容') + download(res.data, row.templateName as string) }).catch(() => { ElMessage.error('下载失败') })