<input ref="file" class="fileUploaderClass" type='file' name="file" id="fileID" webkitdirectory @change.stop="uploadFile"/>
// 上传
uploadFile () {
debugger
this.downloadFiles = this.$refs.file.files // 获取filelist
let formData = new FormData()
formData.append('file', data.file)
for (let i = 0; i < this.downloadFiles.length; i++) { // 每个file append到formdata里
formData.append(this.downloadFiles[i].name,this.downloadFiles[i])
}
this.getUploadedFilesList(formData)
},
// 表单校验
validate () {
return new Promise((resolve, reject) => {
var that = this;
this.form.validateFields((err, fieldsValue) => {
if (!err) {
// 设置上传文件列表
// var downloadFiles = that.downloadFiles.map(o => {
// return o.webkitRelativePath
// })
var downloadFiles = that.downloadFiles;
var files = [];
for(var i=0;i<downloadFiles.length;i++){
files.push(downloadFiles[i].webkitRelativePath);
}
console.log(downloadFiles);
const values = {
...fieldsValue,
'files': files
}
console.log(values);
resolve(values)
} else {
reject(err)
}
})
})
},
// 表单提交的相关操作
async onSubmit () {
debugger
const values = await this.validate()
try {
await this.submitUploadedFilesList(values)
// 执行提交成功的一系列后续操作
this.submitSuccess()
} catch (e) {
// 执行提交失败的一系列后续操作
this.submitFailed(e)
}
},
onCancel(e) {
this.$emit('changeVisible',false)
},
clearFile() {
var oldFile = document.getElementById("fileID");
var newFile = document.createElement("input");
newFile.id = oldFile.id;
newFile.type = "file";
newFile.webkitdirectory = true
oldFile.parentNode.replaceChild(newFile, oldFile);
},
submitSuccess(){
this.$emit('changeVisible',false)
this.form.resetFields();
this.clearFile()
}
接收
@CatchException
@ApiOperation(value ="获取上传文件列表", notes ="获取上传文件列表")
@PostMapping("/getUploadedFilesList")
public CommonResultgetUploadedFilesList(HttpServletRequest request){
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(isMultipart){
ServletFileUpload upload =new ServletFileUpload();
upload.setHeaderEncoding("UTF-8");
int i =0;
try {
FileItemIterator iter = upload.getItemIterator(request);
while(iter.hasNext()){
i++;
FileItemStream fi = iter.next();
InputStream in =null;
OutputStream fileout =null;
try {
String fileName = fi.getName();
//设置保存路径 //TODO 后期换成配置文件
File file =new File("D:\\python\\02-ACMS-code\\Txt\\"+fileName);
if(!file.exists()){
//先得到文件的上级目录,并创建上级目录,在创建文件
file.getParentFile().mkdir();
try {
//创建文件
file.createNewFile();
}catch (IOException e) {
e.printStackTrace();
}
}
in = fi.openStream();
ByteArrayOutputStream bstream =new ByteArrayOutputStream();
Streams.copy(in, bstream, true);
fileout =new FileOutputStream(file);
bstream.writeTo(fileout);
}catch (IOException e) {
throw new RuntimeException("file copy error!",e);
}finally{
if(in !=null){
try {
in.close();
}catch (IOException e) {
e.printStackTrace();
}
}
if(fileout !=null){
try {
fileout.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
}catch (Exception e) {
CommonResult.failed("失败");
throw new RuntimeException("upload file error.",e);
}
}
return CommonResult.success("成功");
}
@CatchException
@ApiOperation(value ="提交上传文件列表", notes ="提交上传文件列表")
@PostMapping("/postUploadedFilesList")
public CommonResultpostUploadedFilesList(@RequestBody UploadParam uploadParam)throws IOException {
try{
String fileDir ="";
String version = uploadParam.getName();