Springboot + Vue + elementUI 文件上传

2020-12-28 10:30

阅读:801

YPE html>

标签:display   als   限制   post   methods   filename   val   pac   button   

Springboot :

1、编写application.properties配置文件

#thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/ // 配置视图解析器前缀路径
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html // 配置视图解析器后缀
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5

#修改tomcat post提交时的文件大小限制
spring.servlet.multipart.enabled=true
spring.servlet.multipart.max-file-size=300MB // 配置上传文件大小和请求文件最大
spring.servlet.multipart.max-request-size=215MB

#配置文件上传路径
File.UpliadFilePath=C:\\UploadFiles\\ // 服务器文件保存路径

2、配置文件对应的实体类,后面配置文件中的参数信息获取直接从该类属性而来
package com.tsht.suma.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
public class FileConfig {

@Value("${File.UpliadFilePath}")
public String UpliadFilePath;


public String getUpliadFilePath() {
return UpliadFilePath;
}

public void setUpliadFilePath(String upliadFilePath) {
UpliadFilePath = upliadFilePath;
}

}
// 为多文件上传
3、编写Controller
@Controller
public class UploadController {

@Autowired
FileConfig fileConfig;
@RequestMapping("/multiUpload")
@ResponseBody
public String multiUpload(HttpServletRequest request,HttpServletResponse response){
List lists = new ArrayList();
// 从配置文件中获取上传到服务器的路径
String filePath = fileConfig.getUpliadFilePath();
//System.out.println(filePath);
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)request;
MultiValueMap multiValueMap = multipartHttpServletRequest.getMultiFileMap();
for(MultiValueMap.Entry> entry: multiValueMap.entrySet()){
List multipartFiles = entry.getValue();
for(MultipartFile multipartFile:multipartFiles){
String filename = multipartFile.getOriginalFilename();
try {
multipartFile.transferTo(new File(filePath + filename));
lists.add(filePath + filename);
System.out.println("上传成功");
}catch (IOException e){
System.out.println(e.getMessage());
}
}
}
return "";
}

Vue + elementUI:


4、index.heml
引入下载在本地的样式文件和js文件




Springboot文件上传
















ref="upload"
name="uploadFile"
action="/multiUpload"
:multiple ="true"
:on-preview="handlePreview"
:on-remove="handleRemove"
:on-change="handleChange"
:file-list="fileList"
:auto-upload="true"
:show-file-list="true">
选取本地文件


文件列表








var Files = [];
var vue = new Vue({
el: ‘#app‘,
data: {
fileList: Files
},
methods:{
indexMethod: indexMethod,
selectFiles:selectFiles,
submitUpload:submitUpload,
handleRemove:handleRemove,
handlePreview:handlePreview,
handleChange:handleChange
},
created: function () {

}
});

function handleChange(file, fileList) {
Files = fileList;
}

//移除文件时
function handleRemove(file, fileList) {
console.log(file, fileList);
}
//添加文件时
function handlePreview(file) {
console.log(file);
}

function submitUpload(){


}
}

Springboot + Vue + elementUI 文件上传

标签:display   als   限制   post   methods   filename   val   pac   button   

原文地址:https://www.cnblogs.com/merfhey/p/13029167.html


评论


亲,登录后才可以留言!