目录
1.后端代码部分:
2.前端代码部分
3.效果展示
1.后端代码部分:
@RestController
@RequestMapping("/file")
public class FileController {
private final String UPLOAD_PATH = "D:/OBS/";//这里写上你需要上传的路径(模拟服务器)
@PostMapping("/upload")
public ResponseEntity<String> uploadFile(@RequestParam MultipartFile file) {
// ... File upload logic ...
if (file.isEmpty()) {
return new ResponseEntity<>("文件不能为空", HttpStatus.BAD_REQUEST);
}
try {
byte[] bytes = file.getBytes();
Path path = Paths.get(UPLOAD_PATH + File.separator + file.getOriginalFilename());
Files.write(path, bytes);
return new ResponseEntity<>("文件上传成功", HttpStatus.OK);
} catch (IOException e) {
e.printStackTrace();
return new ResponseEntity<>("文件上传失败", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
2.前端代码部分
<template>
<div>
<el-upload
drag
action="http://localhost:8081/file/upload"
:on-success="handleUploadSuccess"
:on-error="handleUploadError"
>
<i class="el-icon-upload"></i>
<div class="el-upload__text">拖拽文件到此处上传</div>
</el-upload>
</div>
</template>
<script>
export default {
methods: {
handleUploadSuccess(response, file) {
this.$message.success('文件上传成功');
},
handleUploadError(err, file) {
this.$message.error('文件上传失败');
}
}
};
</script>
3.效果展示
文章来源:https://www.toymoban.com/news/detail-531200.html
文章来源地址https://www.toymoban.com/news/detail-531200.html
到了这里,关于SpringBoot+Vue实现文件上传功能的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!