项目是vue项目,直接贴部分代码了:
html:
<input type="file" name="fileTable" ?@change="changeFile($event)" ref="fileTable">
js:
data(){ ???return { ?????fileTable:‘‘ ????} ?}, ?methods:{//导入文件 ???changeFile(e) { ?????var file = e.target.files[0] ?????var reader = new FileReader() ?????var that = this ?????reader.readAsDataURL(file) ?????reader.onload = function(e) { ???????that.fileTable = this.result; ???????that.uploadFile(); ?????} ???}, ???uploadFile:function(){ ?????if (this.$refs.fileTable.files.length !== 0) { ???????var file = new FormData() ???????file.append(‘uploadExcel‘, this.$refs.fileTable.files[0]) ???????console.log(file); ???????$.ajax({ ?????????url:baseUrl + ‘/usergroup/templateimport‘, ?????????type:‘post‘, ?????????data:file, ?????????datatype: ‘json‘, ?????????cache:false, ?????????traditional: true, ?????????contentType: false, ?????????processData: false, ?????????success: function (res) { ???????????console.log(res); ?????????}, ?????????error: function (error) { ???????????console.log(error); ?????????} ???????}); ?????} ???} ?????}
使用formData上传文件,ajax上传
原文地址:https://www.cnblogs.com/LChenglong/p/9264561.html