前后端传输文件

前端UI使用element的update组件,使用手动上传:

<el-upload
    class="upload-demo"
    action=""
    :auto-upload="false"
    :limit="3"
    :on-exceed="handleExceed"
    :file-list="fileList"
    list-type="picture"
    :on-remove="handleChange"
    :on-change="handleChange">
    	<el-button size="small" type="primary" style="margin: 10px">点击上传</el-button>
</el-upload>
    <el-button style="margin: 10px;" size="small" type="success" @click="updatePictures" >上传到服务器</el-button>

export default {
  data() {
    return {
      fileList: [],
    };
  },
  methods: {
    handleExceed(files, fileList) {
      ElMessage.error("最多展示3个文件!");
    },
    handleChange(file, fileList) {
      this.fileList = fileList;
    },
    async updatePictures() {
		......
    },
  }
};

如果是单个文件传输:

function uploadCourseImage(name,image) {
 let data = new FormData();
 data.append("name", name);
 data.append("file", image.raw);
 return axios.post(`${prefix}/uploadImage`, data, {
   headers: {
     "Content-Type": "multipart/form-data"
   }
 });
}
@PostMapping("/uploadImage")
public OkResponse<String> uploadCourseImage(@RequestPart("name") String name, @RequestPart("file") MultipartFile file) {
	...
}

如果是多个文件传输:

function uploadCarouselChart(files){
  let file = new FormData();
  for (let i=0;i<files.length;i++){
    file.append("files",files[i].raw);
  }
  return axios.post("/admin/uploadImage",file,{
    headers: {
      "Content-Type": "multipart/form-data"
    }
  });
}
@PostMapping("/uploadImage")
public OkResponse<String> uploadCarouselChart(MultipartFile[] files) {
	...
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值