其他分享
首页 > 其他分享> > 文件下载-若依

文件下载-若依

作者:互联网

创建upload表(bookid为关联的业务表主键)

 

 html代码 

<!DOCTYPE html>
<html lang="zh">
<head>
<th:block th:include="include :: header('文件上传')" />
<th:block th:include="include :: bootstrap-fileinput-css" />
<style>
/* 解决layer相册层弹出时导致页面自动滚动 */
html {height: auto;}
</style>
</head>
<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
<div class="row">
<div class="col-sm-12">
<div class="ibox float-e-margins">
<div class="ibox-content">
<div class="form-group">
<label class="font-noraml">文件上传</label>
<div>
<input id="fileinput" name="files" type="file" multiple>
</div>
</div>
<hr>
</div>
</div>
</div>
</div>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: bootstrap-fileinput-js" />
<script th:inline="javascript">
var id = [[${id}]];
var formData = new FormData();
function submitHandler() {
console.log($("#fileinput")[0].files);
if($("#fileinput")[0].files.length == 0){
alert("请选择文件");
return;
}
for (var i= 0;i < $("#fileinput")[0].files.length;i++){
formData.append("file",$("#fileinput")[0].files[i]);
}
formData.append("id",id);
$.ajax({
url:"/system/book/uploadFile",
type:"post",
data:formData,
cache:false,
dataType:"json",
processData:false,
contentType:false,
success:function (result) {
$.operate.successCallback(result);
}
});
}
</script>
</body>
</html>

 controller层代码

@PostMapping("/uploadFile")
@ResponseBody
public AjaxResult uploadFile(@RequestParam("file")MultipartFile[] file,@RequestParam("id")String id){
  //获取yml中配置的下载地址
String filePath = RuoYiConfig.getUploadPath();
Upload upload = null;
try{
for (int i = 0; i < file.length; i++) {
String filePath1 = FileUploadUtils.upload(filePath,file[i]);
upload = new Upload();
upload.setBookId(Long.parseLong(id));
upload.setFileName(file[i].getOriginalFilename());
upload.setPath(filePath1);
iUploadService.insertUpload(upload);
}
}catch (Exception e){
e.printStackTrace();
return AjaxResult.error("文件上传失败!");
}
return AjaxResult.success("文件上传成功");
}

文件上传后

 

 

服务器上传的文件为

 

标签:文件,formData,upload,若依,file,fileinput,上传,id,下载
来源: https://www.cnblogs.com/huatian007/p/16220552.html