当前位置:   article > 正文

vue el-upload上传按fileList文件顺序,一次请求上传多个文件,可携带参数_el-upload file-list

el-upload file-list

解决的问题:

前端往后端法一次请求上传多个文件时,例如后端用MultipartFile[]一次接受多个文件数组。

element-ui的el-upload默认是一个一个上传文件,这时候就需要调用我们自定义文件上传方法利用axios直接进行上传。

 原生手动提交请求(请求多次)

更改后(一次请求)

上传逻辑:

1、:auto-upload="false"  : 设置为 false,表示不自动上传文件,需要手动触发上传。确保在适当的时机手动调用上传方法。

2、:http-request="submitUpload"  这里绑定了上传的请求方法,确保你实现了 submitUpload 方法来处理文件上传的请求。

  1. <el-upload action="#"
  2. ref="upload"
  3. on-remove="handleRemove" 
  4. multiple
  5. :on-change="handleChange"
  6. :on-success="handleAvatarSuccess"
  7. :file-list="fileList"
  8. :http-request="submitUpload"
  9. :auto-upload="false"
  10. }

方法代码:

1、在提交表单时,通过手动调用上传方法 this.$refs.upload.submit() 实现手动触发上传

2、利用FormDate携带文件和参数

3、this.$refs.upload.uploadFiles=this.fileList 确保在submitUpload方法中append文件的顺序和fileList一致

  1. data(){
  2. return{
  3. fileDate:null,
  4. }
  5. }
  6. methods:{
  7. // 表单提交方法
  8. submitForm(){
  9. // 交换位置
  10. if(this.fileList[1]){
  11. [this.fileList[0],this.fileList[1]]=[this.fileList[1],this.fileList[0]]
  12. }
  13. this.fileDate=new FormData() // 创建 FormData 对象
  14. // 将文件列表设置为上传组件的 uploadFiles 属性
  15. this.$refs.upload.uploadFiles=this.fileList
  16. // 手动触发上传 调用submitUpload
  17. this.$refs.upload.submit();
  18. // 将表单参数添加到 FormData 中
  19. this.fileDate.append('parameter',this.uploadForm.parameter)
  20. // 使用 axios 发送POST请求 上传文件
  21. axios({
  22. method: "POST",
  23. url: '#',
  24. data:this.fileDate,
  25. headers: {
  26. '参数': 'XXX',
  27. }
  28. }).then(resp=>{
  29. if(resp.data.code===200){
  30. /*调用axios上传文件后不会触发:on-success的方法
  31. 上传成功后文件后面不会显示对号,需手动设置*/
  32. this.fileList.forEach(file=>{
  33. file.status='success'
  34. })
  35. }
  36. })
  37. },
  38. submitUpload(file){
  39. // 当文件上传时调用的方法,将文件添加到 FormData 中
  40. this.fileDate.append('file',file.file)
  41. },
  42. }

完整代码

输入url即用

  1. <template>
  2. <div class="app-container">
  3. <el-card class="left">
  4. <div>
  5. <el-form :model="uploadForm" ref="uploadForm" label-width="10px"
  6. :rules="uploadFormRules">
  7. <el-form-item prop="parameter">
  8. <el-input size="mini" v-model="uploadForm.parameter" placeholder="请输入参数">
  9. </el-input>
  10. </el-form-item>
  11. <el-form-item prop="file" :style="{marginBottom: '12px'}">
  12. <el-upload action="#"
  13. class="upload-demo"
  14. ref="upload"
  15. :on-remove="handleRemove"
  16. multiple
  17. :on-change="handleChange"
  18. :on-success="handleAvatarSuccess"
  19. :file-list="fileList"
  20. :http-request="submitUpload"
  21. :auto-upload="false"
  22. >
  23. <div class="el-upload-dragger" v-loading="loading"
  24. element-loading-text="正在上传文件"
  25. element-loading-spinner="el-icon-loading"
  26. element-loading-background="rgba(0, 0, 0, 0.8)">
  27. <i class="el-icon-upload" :style="{fontSize:'66px',marginTop:'25px'}"></i>
  28. <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  29. </div>
  30. </el-upload>
  31. <div class="uploadButton" :style="{display:'flex'}">
  32. <el-button v-loading="loading" type="primary"
  33. @click="submitForm" size="mini" :style="{width:'50%'}"
  34. icon="el-icon-upload" v-if="fileList.length>0"
  35. element-loading-text="正在上传文件"
  36. element-loading-spinner="el-icon-loading">上传</el-button>
  37. <el-button type="primary" @click="clearFile" size="mini" :disabled="loading"
  38. :style="{width:'50%'}" icon="el-icon-refresh-left" v-if="fileList.length>0">清空</el-button>
  39. </div>
  40. </el-form-item>
  41. </el-form>
  42. </div>
  43. </el-card>
  44. </div>
  45. </template>
  46. <script>
  47. import axios from 'axios';
  48. export default {
  49. data(){
  50. return{
  51. uploadSuccess:false,
  52. uploadForm:{
  53. file:[],
  54. parameter:null,
  55. },
  56. loading:false,
  57. uploadFormRules:{
  58. parameter:[
  59. { required: true, message: '请输入参数',}
  60. ],
  61. },
  62. fileList:[],
  63. fileDate:null,
  64. }
  65. },
  66. methods:{
  67. clearFile(){
  68. this.fileList=[]
  69. this.uploadSuccess=false
  70. },
  71. submitUpload(file){
  72. this.fileDate.append('file',file.file)
  73. },
  74. submitForm(){
  75. if(!this.loading){
  76. if(this.fileList[1]){
  77. [this.fileList[0],this.fileList[1]]=[this.fileList[1],this.fileList[0]]
  78. }
  79. this.fileDate=new FormData()
  80. this.$refs.upload.uploadFiles=this.fileList
  81. this.$refs.upload.submit();
  82. this.fileDate.append('parameter',this.uploadForm.parameter)
  83. this.loading=true
  84. axios({
  85. method: "POST",
  86. url: '#',
  87. data:this.fileDate,
  88. }).then(resp=>{
  89. console.log(resp)
  90. this.loading=false
  91. if(resp.data.code===200){
  92. this.fileList.forEach(file=>{
  93. file.status='success'
  94. })
  95. }
  96. })
  97. }
  98. },
  99. handleChange(file, fileList){
  100. this.fileList=fileList
  101. this.uploadSuccess=false
  102. },
  103. handleRemove(file, fileList) {
  104. this.fileList=fileList
  105. },
  106. handleAvatarSuccess(resp, file, fileList){
  107. },
  108. },
  109. }
  110. </script>
  111. <style scoped>
  112. .app-container{
  113. width: 100%;
  114. height: 100%;
  115. padding: 0;
  116. margin: 0;
  117. display: flex;
  118. }
  119. .left{
  120. margin-top: 10px;
  121. width: 330px;
  122. margin-left: 40%;
  123. }
  124. .el-upload-dragger {
  125. background-color: #fff;
  126. border: 1px dashed #d9d9d9;
  127. border-radius: 6px;
  128. box-sizing: border-box;
  129. width: 275px;
  130. height: 100%;
  131. text-align: center;
  132. cursor: pointer;
  133. position: relative;
  134. overflow: hidden;
  135. }
  136. .app-container .uploadButton /deep/ .el-loading-mask{
  137. display: flex;
  138. align-items: center;
  139. justify-content:center;
  140. }
  141. .app-container .uploadButton /deep/ .el-loading-spinner{
  142. display: flex;
  143. align-items: center;
  144. justify-content:center;
  145. top:10%;
  146. margin-top: 0;
  147. }
  148. .app-container .el-upload-dragger /deep/ .el-loading-spinner{
  149. top:25%;
  150. margin-top: 0;
  151. }
  152. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/473061
推荐阅读
相关标签
  

闽ICP备14008679号