Vue【七】实现图片上传与预览

news/2024/4/29 19:57:35

前端

图片上传

<template><div class="comment-wrapper"><el-form :model="form" style="    padding: 20px;" label-width="80px"><el-form-item label="评价内容"><el-input v-model="form.comment_content"  type="textarea"></el-input></el-form-item><el-form-item label="上传图片"><el-uploadaction="http://localhost:8081/upload/file"list-type="picture-card":on-preview="handlePictureCardPreview":on-success="uploadSuccess":on-remove="handleRemove"><i class="el-icon-plus"></i></el-upload><el-dialog :visible.sync="dialogVisible"><img width="100%" :src="dialogImageUrl" alt=""></el-dialog></el-form-item><el-form-item style="margin-top: 15px"><el-button type="primary" @click="submitForm">提交评价</el-button></el-form-item></el-form></div>
</template><script>
export default {name: "Comment",data() {return {dialogImageUrl: '',dialogVisible: false,img_url_list:[],form: {comment_username: window.sessionStorage.getItem("token"),comment_content: '',comment_img_url: ''}};},methods: {uploadSuccess(response, file, fileList) {this.img_url_list.push(response.data);},handleRemove(file, fileList) {console.log(1, this.img_url_list);let name = file.response.data;for (let i=0; i<this.img_url_list.length; i++){if (this.img_url_list[i] == name){this.img_url_list.splice(i, 1)}}console.log(1, this.img_url_list);},handlePictureCardPreview(file) {this.dialogImageUrl = file.url;this.dialogVisible = true;},submitForm() {if(this.img_url_list.length > 0){let str = '';for (var i = 0; i < this.img_url_list.length; i++) {str += this.img_url_list[i]+ ",";}str = str.substr(0, str.length - 1);this.form.comment_img_url=str;}this.$http.post("/comment/add", this.form).then(result => {this.$message.success("评论成功");this.$router.push('/order');})}},created() {let orderId = this.$route.query.orderId;this.form.order_id = orderId;}}
</script><style scoped>
.comment-wrapper{margin: 15px;box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);border-radius: 15px;height: 500px;
}
/deep/ .el-form-item{width: 50%;margin: 15px auto;
}
/deep/ .el-textarea__inner{height: 150px;
}
.avatar-uploader {display: flex;justify-content: center;align-items: center;
}.avatar {width: 100px;height: 100px;object-fit: cover;
}.avatar-uploader-icon {font-size: 28px;color: #8c939d;
}
</style>

图片预览

 <div class="comment-img" v-for="img in item.comment_img_list"><img :src="img" @click="open(img)"/></div>
 open(src){this.$alert('<img style="width: 100%" src="'+src+'"></img>', '', {dangerouslyUseHTMLString: true});},

后台代码

package com.wang.wangblog.controller.admin;import com.wang.wangblog.config.Constants;
import com.wang.wangblog.model.Vo.Result;
import com.wang.wangblog.utils.MyBlogUtils;
import com.wang.wangblog.utils.ResultGenerator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;@Controller
@RequestMapping("/admin")
public class UploadController {@Value("${upload.path:E:\\imgs}")private String path;@PostMapping({"/upload/file"})@ResponseBodypublic Result upload(HttpServletRequest httpServletRequest, @RequestParam("file") MultipartFile file) throws URISyntaxException {String fileName = file.getOriginalFilename();String suffixName = fileName.substring(fileName.lastIndexOf("."));//生成文件名称通用方法SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");Random r = new Random();StringBuilder tempName = new StringBuilder();tempName.append(sdf.format(new Date())).append(r.nextInt(100)).append(suffixName);String newFileName = tempName.toString();String month = new SimpleDateFormat("yyyyMM").format(new Date()) + File.separator;String destPath =  path + month;File fileDirectory = new File(destPath);//创建文件File destFile = new File(destPath + newFileName);try {if (!fileDirectory.exists()) {if (!fileDirectory.mkdir()) {throw new IOException("文件夹创建失败,路径为:" + fileDirectory);}}file.transferTo(destFile);} catch (IOException e) {e.printStackTrace();}Result result = ResultGenerator.genSuccessResult();result.setData(MyBlogUtils.getHost(new URI(httpServletRequest.getRequestURL() + "")) + "/upload/" +month+ newFileName);return result;}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.tangninghui.cn.cn/item-12171.htm

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

竞赛 python+opencv+深度学习实现二维码识别

0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; pythonopencv深度学习实现二维码识别 &#x1f947;学长这里给一个题目综合评分(每项满分5分) 难度系数&#xff1a;3分工作量&#xff1a;3分创新点&#xff1a;3分 该项目较为新颖&…

如何使用OpenHarmony实现视频暂停、播放、切换、倍速播放

介绍 本篇Codelab使用ArkTS语言实现视频播放器&#xff0c;主要包括主页面和视频播放页面&#xff0c;我们将一起完成以下功能&#xff1a; 获取本地视频和网络视频。通过AVPlayer进行视频播放。通过手势调节屏幕亮度和视频播放音量。 相关概念 AVPlayer&#xff1a;播放管理…

2024最新版克魔助手抓包教程(9) - 克魔助手 IOS 数据抓包

引言 在移动应用程序的开发中&#xff0c;了解应用程序的网络通信是至关重要的。数据抓包是一种很好的方法&#xff0c;可以让我们分析应用程序的网络请求和响应&#xff0c;了解应用程序的网络操作情况。克魔助手是一款非常强大的抓包工具&#xff0c;可以帮助我们在 Android …

C语言:动态内存管理(上)

目录 前言 1、动态内存函数的介绍 1.1 malloc和free 1.2 calloc函数​编辑 结语 前言 在我们之前对于c语言的学习中&#xff0c;我们知道&#xff0c;当我们要存放数据时&#xff0c;可以定义相应的变量取存放&#xff0c;但是在变量定义后&#xff0c;其大小是无法改变的…

ChatGLM3:AttributeError_ can‘t set attribute ‘eos_token‘

最近在微调 ChatGLM3-6b 时&#xff0c;训练好模型之后&#xff0c;调用inference_hf.py函数验证模型的时候报了如下错误&#xff0c;下面是解决方案。 我在训练时使用的是ptuning_v2.yaml配置文件&#xff0c;训练运行代码如下&#xff1a; CUDA_VISIBLE_DEVICES1 python fi…

typescript 实现RabbitMQ死信队列和延迟队列 订单10分钟未付归还库存

Manjaro安装RabbitMQ 安装 sudo pacman -S rabbitmq rabbitmqadmin启动管理模块 sudo rabbitmq-plugins enable rabbitmq_managementsudo rabbitmq-server管理界面 http://127.0.0.1:15672/ 默认用户名和密码都是guest。 要使用 rabbitmqctl 命令添加用户并分配权限&#xf…