springboot ajax文件上传

前端

<form id="form-add" enctype="multipart/form-data">
	<input type="text" name="name">
	<input type="file" name="file">
  	<button type="button"  onclick="submitFunction()">提交</button>                
</form>

ajax

function submitFunction() {
	    var url = "..........";
        var formData = new FormData($("#form-add")[0]);
	    $.ajax({
			type: "post",
			url: url,
            data : formData,
            cache: false,			 //不需要缓存
            processData : false,  	//必须false才会避开jQuery对 formdata 的默认处理
            contentType : false, 	 //必须false才会自动加上正确的Content-Type
			success:function(data){
				
			},
			error:function(){
				alert("请求出错!");
			}
		});
	}

后端
application.yml

	## 文件大小配置
	spring:
	  servlet:
	    multipart:
	      max-file-size: 20MB	  #单个文件带下
	      max-request-size: 20MB	#总上传数据大小

controller

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    @ResponseBody
    public void uploadFile(HttpServletResponse response, HttpServletRequest request,
               @RequestParam MultipartFile file,
               @RequestParam String name) throws IOException {
        System.out.println(file.isEmpty());
        // 判断是否为空
        /*if (file != null && !file.isEmpty() && file.getSize()!=0) {
            // 上传文件、request请求、保存目录(upload/规范文章管理)、路径前缀
            String filePath = saveFile(file, request, UPLOAD_FILE_PATH, null);
            System.out.println(filePath);
        }*/
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值