• FormData多个文件上传(fileList集合)


    当接口需要文件格式的参数进行文件上传时,前端上传的文件需要使用 FormData

    FormData主要作用:网络请求中处理用来异步的上传文件

    例如:后端需要前端传的参数file但是他是一个集合List

    这里的方式有两种

    1.使用jquery的ajax进行上传

    2.使用axios进行文件上传

    第一种方式实现

    1. js代码
    2. upFiles(formId) {
    3. const Ajaxurl =this.baseUrl + "/attachment/add?relevanceId=" + formId;
    4. const _this = this;
    5. const fileArr = _this.fileList.filter(
    6. (v) => v.url !== "http://baidu.com"
    7. );
    8. // 如果没有新增的附件,就做伪提示
    9. if (_this.fileList.every((v) => v.url === "http://baidu.com")) {
    10. _this.$message({ type: "success", message: "上传成功!" });
    11. _this.saveLoading = false;
    12. return;
    13. }
    14. let formData = new FormData();
    15. fileArr.forEach((item) => {
    16. formData.append("file", item);
    17. });
    18. $.ajax({
    19. type: "POST",
    20. url: Ajaxurl,
    21. dataType: "json",
    22. //jquery上传附件的时候需要将下面两个参数设置成false不然会出现报错信息
    23. processData: false,
    24. contentType: false,
    25. beforeSend: function (XMLHttpRequest) {
    26. XMLHttpRequest.setRequestHeader(
    27. "binding",
    28. _this.getCookie("binding")
    29. );
    30. },
    31. data: fd,
    32. success: function (ajaxResult) {
    33. _this.tableLoading = false;
    34. if (ajaxResult.returnCode == 1) {
    35. _this.$message({
    36. type: "success",
    37. message: "新增成功",
    38. });
    39. _this.loading = false;
    40. _this.saveLoading = false;
    41. setTimeout(() => {
    42. if (window.opener == null) {
    43. window.location.href =
    44. "/query.action?id=" + formId + "&m=query";
    45. } else {
    46. window.opener.location.reload();
    47. window.close();
    48. }
    49. }, 1000);
    50. } else {
    51. _this.$message({
    52. type: "error",
    53. message: ajaxResult.returnInfo,
    54. });
    55. }
    56. },
    57. error: function (err) {
    58. _this.loading = false;
    59. _this.saveLoading = false;
    60. alert("发生错误:" + err.status);
    61. },
    62. });
    63. }

    tipes:当jquery上传的时候将contentType设置成了formdata的时候出现Uncaught TypeError :Illegal invocation 报错

    解决:

    1. $.ajax中的参数重新进行设置
    2. processData: false,
    3. contentType: false,
    4. $.ajax({
    5. type: "POST",
    6. url: Ajaxurl,
    7. dataType: "json",
    8. processData: false,
    9. contentType: false,
    10. beforeSend: function (XMLHttpRequest) {
    11. XMLHttpRequest.setRequestHeader("binding", _this.getCookie("binding"));
    12. },
    13. data: fd,
    14. success: function (ajaxResult) {
    15. 成功干什么
    16. },
    17. error: function (err) {
    18. 失败干什么
    19. alert("发生错误:" + err.status);
    20. }
    21. })

    axios方式实现 

    1. js代码
    2. upFiles(formId) {
    3. //请求的url
    4. const Ajaxurl = this.baseUrl + "/attachment/add?relevanceId=" + formId;
    5. const _this = this;
    6. //重复文件不再上传
    7. const fileArr = _this.fileList.filter(
    8. (v) => v.url !== "http://baidu.com"
    9. );
    10. let formData = new FormData();
    11. //多个文件进行循环添加 formData.append("file", item);多个将会自己转成数组进行上传
    12. fileArr.forEach((item) => {
    13. formData.append("file", item);
    14. });
    15. //请求体
    16. axios
    17. .post(Ajaxurl,
    18. formData, //携带的表单file
    19. {
    20. //当时是表单是的时候需要将请求头设置成multipart/form-data
    21. headers: {
    22. "Content-Type":
    23. "multipart/form-data; boundary=",
    24. binding: _this.getCookie("binding"),
    25. },
    26. }
    27. )
    28. .then((ajaxResult) => {
    29. _this.tableLoading = false;
    30. if (ajaxResult.data.returnCode == 1) {
    31. _this.$message({
    32. type: "success",
    33. message: "新增成功",
    34. });
    35. _this.loading = false;
    36. _this.saveLoading = false;
    37. return
    38. setTimeout(() => {
    39. if (window.opener == null) {
    40. window.location.href =
    41. "/query.action?id=" + formId + "&m=query";
    42. } else {
    43. window.opener.location.reload();
    44. window.close();
    45. }
    46. }, 1000);
    47. } else {
    48. _this.$message({
    49. type: "error",
    50. message: ajaxResult.data.returnInfo,
    51. });
    52. }
    53. })
    54. .catch((error) => {
    55. _this.loading = false;
    56. _this.saveLoading = false;
    57. alert("发生错误:" + err.status);
    58. });
    59. },

     最后看看上传控制台的样子吧

    第一种样子

    ------WebKitFormBoundaryqko4onsawDOCCtSk Content-Disposition: form-data; name="file"; filename="新建文本文档.txt" Content-Type: text/plain   ------WebKitFormBoundaryqko4onsawDOCCtSk--------

    ------WebKitFormBoundaryk4y2P2fXBa25G7XA1d Content-Disposition: form-data; name="code"

    filename="新建文本文档.jpg" Content-Type: multipart/form-data 

    ------WebKitFormBoundaryk4y2P2fXBa25G7XA1d--

    第二种

  • 相关阅读:
    【go】报错整理与解决
    日本亚马逊日本电气产品PSE认证METI备案办理要求
    SVG/CSS路径动画
    iOS上架app store详细教材
    Stream流操作List集合一些常用方法封装
    BGP笔记
    element-plus打开Dialog、图片预览导致页面抖动
    高清LED显示屏设计制作指南
    大数据技术之Hive+Flume+Zookeeper+Kafka详解
    3.Android应用资源:处理配置变更
  • 原文地址:https://blog.csdn.net/Xiaochen1239/article/details/136193649