上传文件及文件夹

实现效果:

 想要实现可以上传文件和文件夹这个操作,

实现代码:

1.html:

<el-upload
  class="upload-demo"
  action="https://jsonplaceholder.typicode.com/posts/"
  :http-request="modeUpload">
  <el-button size="small" type="primary">选择文件</el-button>
</el-upload>

<el-button size="small" type="primary" @click="chooseFiles">选择文件夹</el-button>
<div v-show="false">
  <input type="file" id="file" ref="file" webkitdirectory directory multiple="multiple" v-on:change="handleFileUpload($event)" />
</div>

2.data:

modeList: '',

3.methods:

//选择文件
modeUpload: function(item) {
  this.modeList = item.file;
},

//触发文件夹选择
chooseFiles(e){
  document.getElementById('file').click();
},

//选择文件夹
handleFileUpload(event){
  this.modeList = document.getElementById("file").files;
},

4.上传:

let formData = new FormData();

// 把文件信息添加进如对象
formData.append('file', this.modeList);
扫描二维码关注公众号,回复: 10312203 查看本文章

注意:

1.选择了文件夹后,默认选择了该文件夹下的所有文件。

猜你喜欢

转载自www.cnblogs.com/5201314m/p/12598809.html