vue elementui springbootアップロードファイル
3371 ワード
vueセクション:
対応するjs:
アップロードトリガの方法:
バックグラウンドコントロール:
主なコード:
2M jpg/png/excel/word/txt/ppt/pdf
対応するjs:
data() {
return {
fileList: [], //
experiment: {
file: '',
},
},
},
methods:{
beforeRemove(file) {
return this.$confirm(` ${file.name}?`);
},
beforeUpload(file) {
// experiment, Url ,
this.experiment.file = file;
const isLt2M = file.size / 1024 / 1024 < 2;
if (!isLt2M) {
this.$message.error(' 2MB!');
}
return isLt2M;
},
afterRemove() {
this.fileList = [];
this.experiment.file = '';
},
}
アップロードトリガの方法:
const fd = new FormData();
fd.append('reportFile', reportFile);
fd.append('experimentReport', JSON.stringify(experimentReport));
const config = {
headers: {
'Content-Type': 'multipart/form-data',
},
};
return this.$axios.$put(experimentReportUri.updateUrl, fd, config);
バックグラウンドコントロール:
@PutMapping
ResponseEntity updateReport(KeycloakAuthentication principal,
@RequestParam(value = "experimentReport") String experimentReport,
@RequestParam(value = "reportFile", required = false) MultipartFile reportFile) throws IOException;
主なコード:
String staticPath = ResourceUtils.getURL("classpath:static").getPath();
// static F:/workplace/ideaPlace/imbs-e/target/classes/static
staticPath = staticPath.substring(1);
System.out.println(" static "+staticPath);
String fileName = reportFile.getOriginalFilename();
// ( - - )
LocalDate now = LocalDate.now();
// now().toString() 2019-11-14
String uploadFoldName = staticPath + File.separator + now.toString();
//
File uploadFold = new File(uploadFoldName);
if (!uploadFold.exists() || !uploadFold.isDirectory()) {
//
uploadFold.mkdirs();
}
// ( linux windows \ /, File.separator)
File file = new File(uploadFold + File.separator + fileName);
reportFile.transferTo(file);
// F:\workplace\ideaPlace\imbs-e\target\classes\static\2019-11-13\jixu.txt
String getSavePath = file.getAbsolutePath();
System.out.println(" :"+getSavePath);
// \static\2019-11-13\jixu.txt
String getWebPath = getSavePath.substring(getSavePath.lastIndexOf("static") - 1);
System.out.println(" uri :"+getWebPath);