ajax springMvc 异步 上传 文件

很久没有写文件上传的代码了,以下代码支持异步上传,备注一下。

 

1.页面设置 

<div>
<input type="file" class="easyui-linkbutton" id="excelFile" name="excelFile" size="20" maxlength="20"/>

<input type="button" class="easyui-linkbutton" onClick="uploadFile();" style="width:80px" value="上传" />

</div>

  

 

2. js 代码

    function uploadFile() {
    	var url = "upload.htm?method=upload')";

		var formData = new FormData();
		formData.append('excelFile', $('#excelFile')[0].files[0]);
		jQuery.ajax({
		    url: url,
		    type: 'POST',
		    cache: false,
		    data: formData,
		    processData: false,
		    contentType: false
		}).done(function(res) {
			console.log(res);
		}).fail(function(res) {
			console.log("failed="+res);
		});
    }
    
    </

  

3. Action code

 @RequestMapping(value="/upload.htm", params = "method=upload")
	public String upload(@RequestParam("excelFile") MultipartFile file, ModelMap model,HttpServletResponse response) {
    
    String fileName = file.getOriginalFilename();
    InputStream is = file.getInputStream();
}

  

posted @ 2017-10-25 16:08  椰香飘逸  阅读(177)  评论(0编辑  收藏  举报