vue前端图片上传、回显图片

<label>文件:</label>
<input type="file" @change="selectFile($event)"/>
<button type="button" @click="upfile($event)">上传</button>
<div>
    <img :src="'upfiles/'+filename" />
</div>
<script src="js/axios.min.js"></script>
    <script src="js/vue.js"></script>
    <script>
        const vm = new Vue({
            el:"#app",
            data:{
                file:null,
                filename:"shangchuang.png"
            },
            methods: {
                selectFile(event){
                    console.log(event);
                    this.file = event.target.files[0];
                },
                upfile(event){
                    let formdata = new FormData();
                    formdata.append("file",this.file);
                    axios.post("http://localhost:8080/upfile/photo",formdata,{
                        "Content-type":"multipart/form-data"
                    }).then((res)=>{
                        console.log(res.data);
                        this.filename = res.data;
                    });
                }
            },
        })
    </script>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要在前端页面使用 el-upload 组件上传图片,并将图片的 base64 编码作为参数发送到后端。 然后,在后端使用 Spring Boot 编写接口,接收前端传来的图片的 base64 编码,将其解码成字节数组,再将字节数组转换成文件保存到服务器上。 在接口返回时,将图片的 URL 返回给前端前端可以使用该 URL 在浏览器中显示图片。 下面是一个简单的示例代码: 前端代码: ```vue <template> <div> <el-upload class="upload-demo" action="/uploadImage" :on-success="handleSuccess" :before-upload="beforeUpload" :headers="{ Authorization: 'Bearer ' + token }" :data="{ userId: 123 }" :show-file-list="false" > <el-button size="small" type="primary">点击上传</el-button> </el-upload> <img :src="imageUrl" v-if="imageUrl" /> </div> </template> <script> export default { data() { return { imageUrl: "", token: "your_token", }; }, methods: { handleSuccess(response, file, fileList) { this.imageUrl = response.url; }, beforeUpload(file) { const isJPG = file.type === "image/jpeg"; const isPNG = file.type === "image/png"; const isLt2M = file.size / 1024 / 1024 < 2; if (!isJPG && !isPNG) { this.$message.error("上传图片只能是 JPG/PNG 格式!"); return false; } if (!isLt2M) { this.$message.error("上传图片大小不能超过 2MB!"); return false; } return true; }, }, }; </script> ``` 后端代码: ```java @RestController public class UploadController { @PostMapping("/uploadImage") public String uploadImage(@RequestParam("file") String base64Image, @RequestParam("userId") Long userId) throws IOException { // 解码 base64 图片 String[] base64ImageParts = base64Image.split(","); byte[] decodedBytes = Base64.getDecoder().decode(base64ImageParts[1]); // 保存图片到服务器 String fileName = userId + "-" + UUID.randomUUID().toString() + ".jpg"; Path path = Paths.get("/path/to/save/" + fileName); Files.write(path, decodedBytes); // 返回图片 URL return "http://your_domain.com/" + fileName; } } ``` 注意,这只是一个简单的示例代码,你需要根据自己的实际需求进行适当的修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值