el-upload 上传图片及回显照片和预览图片,文件流和http线上链接格式操作

本文主要是介绍el-upload 上传图片及回显照片和预览图片,文件流和http线上链接格式操作,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入图片描述
在这里插入图片描述

   <div  v-for="(info, index) in zsjzqwhxqList.helicopterTourInfoList"  :key="info.id" >编辑上传图片// oss返回线上地址http链接格式:<el-form-itemlabel="巡视结果照片":label-width="formLabelWidth"><el-upload:action="'http://×××××uploadFile'"list-type="picture-card":limit="10":on-exceed="limitError":on-success="(response, file, fileList) =>imgSuccess(response,file, fileList,index ) ":on-error="imgError":on-remove="(file, fileList) =>handleRemove(file,fileList, index) ":file-list="getFileList(info.url)":on-preview="handlePreview"class="horizontal-upload"  ></el-upload></el-form-item>//文件流格式:<el-col :span="24"><el-form-itemlabel="巡视结果照片":label-width="formLabelWidth"><el-upload:action="'×××接口地址/uploadFile' "list-type="picture-card":limit="10":on-exceed="limitError":on-success="(response, file, fileList) =>imgSuccess(response,file,fileList,index) ":on-error="imgError":on-remove="(file, fileList) =>handleRemove(file,fileList,index ) ":on-preview="handlePreview":file-list="getFileList(index)"class="horizontal-upload" ></el-upload></el-form-item></el-col>

然后就是方法了有2种

     //http在线返回:返回的是http形式直接浏览器可以打开// async xiaziaT() {//     for (const tourInfo of this.zsjzqwhxqListck//         .helicopterTourInfoList) {//         if (tourInfo.url) {//             const urls = tourInfo.url.split(",");//             for (const url of urls) {//                 try {//                     const data = await getFileUrl({ url });//                     console.log(data);//                     // 更新 tourInfo.imageUrl 以回显图片//                     tourInfo.imageUrl = data.data.msg;//                 } catch (error) {//                     console.error(`无法下载文件 ${url}`, error);//                 }//             }//         }//     }// },//文件流:后端接口返回文件流格式async xiaziaTwh() {this.teamPhotoUrls = [];const tempTeamPhotoUrls = [];for (const [tourIndex,tourInfo,] of this.zsjzqwhxqList.helicopterTourInfoList.entries()) {if (tourInfo.url) {console.log(tourInfo.url, "tourInfo.url");const urls = tourInfo.url.split(",");tempTeamPhotoUrls[tourIndex] = [];for (const url of urls) {try {const data = await downloadFile({ url });let blob = new Blob([data], {type: "image/jpg",});console.log(data);const imageUrl = data.data.msg;tempTeamPhotoUrls[tourIndex].push({name: url.split(",").pop(),url: imageUrl,});console.log(this.fileList, "234");} catch (error) {console.error(`无法从下载文件 ${url}`, error);}}}}this.teamPhotoUrls = tempTeamPhotoUrls;},

公共方法:

 data() {return {fileList: [],fileListCache: {},zsjzqwhxqList: [],zsjzqwhxqListck: [],},
http在线方法:// getUrls(urlString) {//     if (typeof urlString !== "string" || !urlString) {//         return [];//     }//     return urlString.split(",");// },// getFileList(urlString) {//     if (this.fileListCache[urlString]) {//         return this.fileListCache[urlString];//     }//     const urls = this.getUrls(urlString);//     const fileList = urls.map((url, index) => ({//         name: `图片 ${index + 1}`,//         url: url,//     }));//     this.fileListCache[urlString] = fileList;//     return fileList;// },
//防止上传和回显图片了重复加载,因为是根据url:'里面有几个url,隔开会请求几遍文件流地址避免重复渲染'computed: {computedFileList() {return this.teamPhotoUrls.map((photos, index) => {return photos.map((photo) => ({name: photo.name,url: photo.url,status: "done", // 设置文件状态为已完成}));});},
//上传功能
limitError(files, fileList) {this.$message.error("最多只能上传10张图片");},imgSuccess(response, file, fileList, index) {console.log(file, fileList);// 获取当前的 URL 列表let currentUrls =this.zsjzqwhxqList.helicopterTourInfoList[index].url || "";// 将新上传的文件 URL 添加到当前的 URL 列表中const newUrl = file.response.msg;if (currentUrls) {currentUrls += "," + newUrl;} else {currentUrls = newUrl;}// 更新 URL 列表和文件列表this.zsjzqwhxqList.helicopterTourInfoList[index].url = currentUrls;this.zsjzqwhxqList.helicopterTourInfoList[index].fileList = fileList;this.$message.success("文件上传成功");// this.$nextTick(() => {// this.xiaziaTwh();// });},imgError(err, file, fileList) {this.$message.error("文件上传失败");},limitError(files, fileList) {this.$message.error("上传文件数量超出限制");},// 删除图片handleRemove(file, fileList, index) {console.log("Removing file:", file);console.log("Removing fileList:", fileList);console.log("Removing index:", index);const url ="××接口地址:10007/zxjcgjgl/helicopterTourManagement/deleteImage";let imageUrl;if (file.raw) {imageUrl = file.response.msg;console.log("6666");} else {imageUrl = file.name;console.log("7777");} fetch(url, {method: "POST",headers: {"Content-Type": "application/json",},body: JSON.stringify({ url: imageUrl }),}).then((response) => response.json()).then((data) => {console.log("响应数据:", data);let currentUrls = this.zsjzqwhxqList.helicopterTourInfoList[index].url;console.log("删除前的当前URL:", currentUrls);if (data.code === 200) {if (currentUrls) {let urlArray = currentUrls.split(",");let newUrlArray = urlArray.filter((url) => url !== imageUrl);this.zsjzqwhxqList.helicopterTourInfoList[index ].url = newUrlArray.join(",");}this.zsjzqwhxqList.helicopterTourInfoList[index].fileList = fileList;this.$message.success("图片删除成功");} else {console.error("错误:", data);this.$message.error("图片删除失败");}}).catch((error) => {console.error("错误", error);this.$message.error("图片删除失败");});},
//预览图片:handlePreview(file) {this.$alert(`<img src="${file.url}" style="width: 100%;">`,"图片预览",{dangerouslyUseHTMLString: true,confirmButtonText: "关闭",});},

这篇关于el-upload 上传图片及回显照片和预览图片,文件流和http线上链接格式操作的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1089901

相关文章

关于MongoDB图片URL存储异常问题以及解决

《关于MongoDB图片URL存储异常问题以及解决》:本文主要介绍关于MongoDB图片URL存储异常问题以及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录MongoDB图片URL存储异常问题项目场景问题描述原因分析解决方案预防措施js总结MongoDB图

Python ZIP文件操作技巧详解

《PythonZIP文件操作技巧详解》在数据处理和系统开发中,ZIP文件操作是开发者必须掌握的核心技能,Python标准库提供的zipfile模块以简洁的API和跨平台特性,成为处理ZIP文件的首选... 目录一、ZIP文件操作基础三板斧1.1 创建压缩包1.2 解压操作1.3 文件遍历与信息获取二、进阶技

Java中字符串转时间与时间转字符串的操作详解

《Java中字符串转时间与时间转字符串的操作详解》Java的java.time包提供了强大的日期和时间处理功能,通过DateTimeFormatter可以轻松地在日期时间对象和字符串之间进行转换,下面... 目录一、字符串转时间(一)使用预定义格式(二)自定义格式二、时间转字符串(一)使用预定义格式(二)自

python实现svg图片转换为png和gif

《python实现svg图片转换为png和gif》这篇文章主要为大家详细介绍了python如何实现将svg图片格式转换为png和gif,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录python实现svg图片转换为png和gifpython实现图片格式之间的相互转换延展:基于Py

使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)

《使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)》PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域,PPT文档中常常包含丰富的图片内容,这些图片不仅提升了... 目录一、引言二、环境与工具三、python 提取PPT背景图片3.1 提取幻灯片背景图片3.2 提取

Python实现图片分割的多种方法总结

《Python实现图片分割的多种方法总结》图片分割是图像处理中的一个重要任务,它的目标是将图像划分为多个区域或者对象,本文为大家整理了一些常用的分割方法,大家可以根据需求自行选择... 目录1. 基于传统图像处理的分割方法(1) 使用固定阈值分割图片(2) 自适应阈值分割(3) 使用图像边缘检测分割(4)

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

HTML5表格语法格式详解

《HTML5表格语法格式详解》在HTML语法中,表格主要通过table、tr和td3个标签构成,本文通过实例代码讲解HTML5表格语法格式,感兴趣的朋友一起看看吧... 目录一、表格1.表格语法格式2.表格属性 3.例子二、不规则表格1.跨行2.跨列3.例子一、表格在html语法中,表格主要通过< tab

springboot上传zip包并解压至服务器nginx目录方式

《springboot上传zip包并解压至服务器nginx目录方式》:本文主要介绍springboot上传zip包并解压至服务器nginx目录方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录springboot上传zip包并解压至服务器nginx目录1.首先需要引入zip相关jar包2.然

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel