当前位置:   article > 正文

Upload 上传文件 设置请求参数

Upload 上传文件 设置请求参数

 

 

不同的数据类型需要设置不同的 content-type 在表单中进行上传的时候则需要设置 content-type:form-data

  • 比方说上传图片:
  1. <el-upload
  2. class="avatar-uploader"
  3. action=""
  4. accept="image/jpeg, image/png"
  5. :auto-upload="false"
  6. :show-file-list="false"
  7. :file-list="addDetaile.qrcodeImg"
  8. :on-change="uploadSectionFile"
  9. >
  10. <el-button slot="trigger" size="medium" type="primary"
  11. >上传</el-button
  12. >
  13. <el-button
  14. type="danger"
  15. size="medium"
  16. @click="removeImg('eq')"
  17. >删除</el-button
  18. >
  19. <p>建议上传200*200的图片</p>
  20. </el-upload>
  1. uploadSectionFile(file) {
  2. this.uploadImg(file, "eq");
  3. },
  4. uploadImg(file, status) {
  5. if (status == "tx") {
  6. this.addDetaile.coverImg = [file];
  7. } else {
  8. this.addDetaile.qrcodeImg = [file];
  9. }
  10. let param = new FormData();
  11. // 上传文件对象 名称file与后台控制器参数要一致
  12. param.append("file", file.raw);
  13. this.$http({
  14. headers: { "Content-Type": "multipart/form-data" },
  15. url: this.$http.adornUrl("/file/upload"),
  16. method: "post",
  17. data: param,
  18. }).then(({ data }) => {
  19. if (data && data.code === 200) {
  20. if (status == "tx") {
  21. this.coverImg = data.data; // 图片上传回显
  22. } else {
  23. this.qrcodeImg = data.data; // 图片上传回显
  24. }
  25. } else {
  26. this.$message({
  27. message: data.message,
  28. type: "error",
  29. duration: 1500,
  30. });
  31. }
  32. });
  33. },
  34. removeImg(str) {
  35. if (str == "tx") {
  36. this.coverImg = "";
  37. } else {
  38. this.qrcodeImg = "";
  39. }
  40. },
  • 上传表格数据
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号