vue图片上传功能

element-ui提供了上传的组件,真是方便,el-upload

情景:

界面上虽然看到选取后上传到界面了,但是没有传到后端,F12->network可以看到400

原因:

它自己内部封装了一套ajax请求,而不通过axios请求,这样就会导致上传不成功,因为默认没有携带token,所有的api接口除了登录,只要通过axios方式进行请求,就会通过请求拦截器就行携带token,当然这个请求拦截器是自己配置的

解决:

上传组件el-upload手动绑定一个属性  :headers="headerObj"

data中定义

// 图片上传组件的请求头对象

      headerObj: {

        Authorization: window.sessionStorage.getItem('token')

      }

上传成功之后会返回一个临时路径值

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现 Spring Boot 后端和 Vue.js 前端的图片上传功能可以分为以下几个步骤: 1. 创建 Spring Boot 后端接收图片的 API。可以使用 Spring Boot 的 `@PostMapping` 注解和 `@RequestParam` 注解来接收上传图片文件,然后将文件保存到服务器的本地磁盘或者云存储中,返回保存成功的图片路径。 示例代码: ```java @PostMapping("/uploadImage") public String uploadImage(@RequestParam("file") MultipartFile file) throws IOException { // 保存文件 String fileName = file.getOriginalFilename(); String filePath = "/path/to/image/directory/" + fileName; File dest = new File(filePath); file.transferTo(dest); // 返回保存成功的图片路径 return filePath; } ``` 2. 创建 Vue.js 前端上传图片的组件,并在组件中使用 `axios` 库发送请求到后端接口上传图片文件。 示例代码: ```vue <template> <div> <input type="file" ref="fileInput" @change="handleFileChange"> <button @click="uploadImage">上传图片</button> </div> </template> <script> import axios from 'axios'; export default { data() { return { file: null, imageUrl: '' } }, methods: { handleFileChange(event) { this.file = event.target.files[0]; }, uploadImage() { let formData = new FormData(); formData.append('file', this.file); axios.post('/api/uploadImage', formData, { headers: { 'Content-Type': 'multipart/form-data' } }).then(response => { this.imageUrl = response.data; console.log('Image uploaded successfully: ' + this.imageUrl); }).catch(error => { console.log('Error uploading image: ' + error); }); } } } </script> ``` 其中 `handleFileChange` 方法会在用户选择要上传图片文件被触发,将选中的文件保存到组件的 `file` 属性中。`uploadImage` 方法会创建一个 `FormData` 对象,将组件的 `file` 属性添加到其中,然后使用 `axios` 发送 POST 请求到后端接口上传图片文件,并将上传成功后返回的图片路径保存到组件的 `imageUrl` 属性中。 3. 在 Vue.js 前端页面中显示上传成功的图片。 示例代码: ```vue <template> <div> <input type="file" ref="fileInput" @change="handleFileChange"> <button @click="uploadImage">上传图片</button> <img :src="imageUrl"> </div> </template> <script> import axios from 'axios'; export default { data() { return { file: null, imageUrl: '' } }, methods: { handleFileChange(event) { this.file = event.target.files[0]; }, uploadImage() { let formData = new FormData(); formData.append('file', this.file); axios.post('/api/uploadImage', formData, { headers: { 'Content-Type': 'multipart/form-data' } }).then(response => { this.imageUrl = response.data; console.log('Image uploaded successfully: ' + this.imageUrl); }).catch(error => { console.log('Error uploading image: ' + error); }); } } } </script> ``` 其中 `<img :src="imageUrl">` 标签用来显示上传成功的图片,其 `src` 属性绑定到组件的 `imageUrl` 属性,当 `imageUrl` 属性更新图片会自动刷新。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值