当前位置:   article > 正文

vue实现文件上传,FormData传入后台_vue上传文件到后端

vue上传文件到后端

 单文件,多文件看需求都没问题,前端代码弹窗界面如下:

:visible.sync="excelVisible" 默认false自定义,ture开始弹窗,改变值是uploadaaa()

  1. <!-- excel上传弹窗 -->
  2. <el-dialog title="excel上传"
  3. :visible.sync="excelVisible"
  4. width="40%"
  5. :show-close="false"
  6. :before-close="handleClose">
  7. <el-form label-width="140px"
  8. label-position="right">
  9. <el-row>
  10. <el-col>
  11. <el-form-item label="上传文件"
  12. prop="fileName">
  13. <el-input v-model="fileName"
  14. :readonly='true'
  15. placeholder="文件名"
  16. style="width:300px;">
  17. </el-input>
  18. <el-upload class="upload-demo"
  19. ref="upload"
  20. action="doUpload"
  21. :limit="1"
  22. :file-list="fileList"
  23. style="float:right"
  24. :before-upload="beforeUpload">
  25. <el-button slot="trigger"
  26. type="primary"
  27. plain>选取文件</el-button>
  28. </el-upload>
  29. </el-form-item>
  30. </el-col>
  31. </el-row>
  32. <el-row>
  33. <el-col :span="24">
  34. <div class="btns-center mb-48 pt-48" style="float:right">
  35. <el-button @click="handleCloses">取 消</el-button>
  36. <el-button type="primary"
  37. @click="submitUpload">确 定</el-button>
  38. </div>
  39. </el-col>
  40. </el-row>
  41. </el-form>
  42. </el-dialog>

methods:

  1. 自定义,这里我除去加了excelVisible 弹窗控制还有其他需求
  2. uploadaaa () {
  3. this.excelVisible = true;
  4. var _this = this
  5. _this.parameterDto = {
  6. locationCode: '',
  7. steelType: '',
  8. locationMaxNum: '',
  9. current: 1,
  10. size: 10
  11. }
  12. this.listLoading = true
  13. selectBucketList(_this.parameterDto).then(response => {
  14. if (response.code === 200) {
  15. debugger
  16. this.bucketList = response.data
  17. this.total = response.count
  18. this.listLoading = false
  19. }
  20. })
  21. //复原
  22. var _this = this
  23. _this.parameterDto = {
  24. locationCode: '',
  25. steelType: '',
  26. locationMaxNum: '',
  27. current: 1,
  28. size: 10
  29. }
  30. this.listLoading = true
  31. selectMinIoList(_this.parameterDto).then(response => {
  32. console.log(response)
  33. debugger
  34. if (response.code === 200) {
  35. this.locationList = response.data
  36. this.total = response.count
  37. this.listLoading = false
  38. }
  39. })
  40. //还原上传弹窗
  41. this.bucket = '';
  42. this.show5 = false;
  43. this.show6 = false;
  44. this.show7 = false;
  45. },
  46. 上传文件检查文件格式并且赋值filename
  47. beforeUpload (file) { // 上传前 文件校验
  48. this.files = file;
  49. debugger
  50. // 1
  51. // const extension = file.name.split('.')[1] === 'xls'
  52. // const extension2 = file.name.split('.')[1] === 'xlsx'
  53. // const isLt2M = file.size / 1024 / 1024 < 2
  54. // if (!extension && !extension2) {
  55. // this.$message.warning('上传文件只能是 xls、xlsx格式!')
  56. // return
  57. // }
  58. // if (!isLt2M) {
  59. // this.$message.warning('上传文件大小不能超过 2MB!')
  60. // return
  61. // }
  62. // this.fileName = file.name;
  63. //
  64. this.fileName = file.name;
  65. this.showVisible = true;
  66. this.show5 = true;
  67. return false // 返回false不会自动上传
  68. },
  69. 文件确认上传准备进入后台
  70. submitUpload (bucket) { // 确认上传
  71. if(bucket == ""){
  72. this.$message({
  73. message: '请选择要上传的目录!',
  74. type: 'warning',
  75. })
  76. return false;
  77. }
  78. if (this.fileName == "") {
  79. this.$message({
  80. message: '请选择要上传的文件!',
  81. type: 'warning',
  82. })
  83. return false;
  84. }
  85. debugger
  86. let fileFormData = new FormData();
  87. this.detailsVisibles = true;
  88. this.excelVisible = false;
  89. fileFormData.append('file', this.files,this.fileName); // 上传的文件
  90. fileFormData.append('file', this.files,this.fileNames); // 上传的文件2
  91. fileFormData.append("bucket",bucket)
  92. console.log(fileFormData)
  93. this.loading = true;
  94. uploadEmployees(fileFormData).then(res => { // 上传请求
  95. if (res.code == 200) {
  96. this.$message({
  97. type: 'success',
  98. message: '上传成功!'
  99. })
  100. this.listLoading = true
  101. selectMinIoList(this.parameterDto).then(response => {
  102. this.locationList = response.data
  103. this.total = response.count
  104. this.listLoading = false
  105. })
  106. this.detailsVisibles = true
  107. console.log("1="+this.list1[0])
  108. console.log("2="+this.list2)
  109. console.log("3="+this.list3)
  110. this.excelVisible = false;
  111. }
  112. }).catch(err => {
  113. this.fileName = '';
  114. this.fileNames = '';
  115. console.log(err);
  116. this.loading = false;
  117. })
  118. },

vue请求地址

后台就是注意类头代码基本就是按需求来了,多文件MultipartFile记得带[ ],单文件可以不用也可以带

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