el-upload---上传/下载图片
上传照片
<el-upload ref="upload" :file-list="fileList" action="#" :auto-upload="false" :on-change="handleChange" :on-remove="handleRemove" :limit="2" list-type="picture-card" > <i class="el-icon-plus" :style="{ display: fileList.length ? 'none' : 'inline-block' }" ></i> <span :style="{ display: fileList.length ? 'inline-block' : 'none' }" >点击重新选择</span > </el-upload>
data: () => {
return {
newData: {},
fileList: [], //图片信息
};
},
// 上传照片
methods:{
async addData() { let condition = new FormData(); condition.append("fileId", this.info.fileId); condition.append("id", this.info.id); condition.append("comment", this.comment); condition.append("files", this.fileList[0]?.raw || await this.Base64ToFile(this.fileList[0]?.url, this.fileList[0]?.name)); uploadFile(condition) .then((res) => { if (res.data.code === 200) { this.$message.success("上传成功!"); this.$emit("successHandle") } else { this.$message.error(res.data.message); } }) .catch((error) => { this.$message.error({ message: failResponse(error).message, }); }); }, // 将base64转换为file Base64ToFile(dataurl, fileName) { const arr = dataurl.split(","), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]); let n = bstr.length, u8arr = new Uint8Array(n); while (n--) { u8arr[n] = bstr.charCodeAt(n); } return new File([u8arr], fileName, { type: mime }); },
handleChange(files, fileList) {
if (fileList.length > 1) { //限制只可上传一张
fileList.splice(0, 1);
}
this.fileList = fileList;
},
handleRemove(files, fileList) {
this.fileList = [];
},
}
查看照片
// 查看照片 getData() { getFile({ fileId: this.info.fileId, }) .then((res) => { this.comment = res.data.data.comment; this.fileList = [{name: res.data.data.fileName, url: 'data:image/png;base64,'+this.ArrayBufferToBase64(res.data.data.files)}] }) .catch((error) => { this.$message.error({ message: failResponse(error).message, }); }); }, // byte数组转字符串 ArrayBufferToBase64(buffer) { // 第一步:将arrayBuffer转为二进制字符串 let binary = ''; let bytes = new Uint8Array(buffer); for (let len = bytes.byteLength, i =0; i<len;i++) { binary += String.fromCharCode(bytes[i]); } // 将二进制字符串转为base64字符串 return window.btoa(binary); },
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具