示例

代码
| <template> |
| <div class="preview" @click="onClick"> |
| <div class="preview-img"> |
| <div class="opt-box"> |
| <CloudDownloadOutlined :style="{fontSize: '44px', color: '#0d71ff'}" title="下载" @click="handleDownload"/> |
| <CloseOutlined :style="{fontSize: '44px', color: '#0d71ff'}" title="关闭" @click="onClick"/> |
| </div> |
| <img :src="src" alt="预览图片" /> |
| </div> |
| </div> |
| </template> |
| <script setup> |
| import { CloseOutlined, CloudDownloadOutlined } from '@ant-design/icons-vue' |
| |
| const props = defineProps({ |
| src: { |
| type: String, |
| default: '', |
| required: true |
| }, |
| srcName: { |
| type: String, |
| default: '', |
| required: true |
| }, |
| onDownload: { |
| type: Function, default: () => { |
| }, required: true |
| }, |
| onClick: { |
| type: Function, default: () => { |
| }, required: true |
| }, |
| onKeydown: { |
| type: Function, default: () => { |
| }, required: true |
| } |
| }) |
| const {onDownload, src, srcName} = props |
| const handleDownload = (e) => { |
| e.stopPropagation() |
| onDownload({attachmentUrl: src, attachmentName: srcName}) |
| } |
| </script> |
| <style lang="scss" scoped> |
| .preview { |
| width: 100%; |
| height: 100%; |
| background: rgba(0, 0, 0, 0.5); |
| position: fixed; |
| top: 0; |
| bottom: 0; |
| left: 0; |
| right: 0; |
| z-index: 9999; |
| overflow: scroll; |
| cursor: pointer; |
| display: flex; |
| align-items: center; |
| } |
| |
| .preview-img { |
| padding: 20px; |
| display: inline-block; |
| margin: auto; |
| position: relative; |
| |
| img { |
| max-width: 100%; |
| max-height: 100%; |
| } |
| |
| .opt-box { |
| position: absolute; |
| top: 34px; |
| right: 40px; |
| |
| span { |
| margin-left: 14px; |
| } |
| } |
| } |
| </style> |
| <ImgPreview |
| v-if="previewVisible" |
| :src="previewUrl" |
| :srcName="previewName" |
| :onClick="closeImgPreview" |
| :onDownload="onDownLoad" |
| /> |
| |
| |
| const previewVisible = ref(false) |
| const previewUrl = ref('') |
| const previewName = ref('') |
| const onPreview = ({ attachmentUrl,attachmentName }) => { |
| if(isEmpty(attachmentUrl)) return |
| previewVisible.value = true |
| previewUrl.value = attachmentUrl |
| previewName.value = attachmentName |
| } |
| |
| const closeImgPreview = () => { |
| previewVisible.value = false; |
| } |
| |
| const onDownLoad = ({ attachmentUrl,attachmentName }) => { |
| if(isEmpty(attachmentUrl)) {return} |
| let param = { |
| name: attachmentName.split('.')[0], |
| extName: attachmentName.split('.')[1], |
| address: attachmentUrl, |
| } |
| doDownload(param) |
| |
| } |
| |
| |
| export function doDownload(item) { |
| let aUrl = item.address; |
| let extNameIcon = item.extName; |
| let extName = item.name; |
| if (extName.includes(".")) { |
| extName = extName.split(".")[0]; |
| } |
| let xml = new XMLHttpRequest(); |
| xml.open("GET", aUrl, true); |
| xml.responseType = "blob"; |
| xml.onload = () => { |
| let url = window.URL.createObjectURL(xml.response); |
| let a = document.createElement("a"); |
| a.href = url; |
| a.download = `${extName}.${extNameIcon}`; |
| a.style.display = "none"; |
| a.click(); |
| }; |
| xml.send(); |
| } |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具