spring mvc ajax file,如何使用ajax文件上傳和spring mvc上傳文件?

I have a jsp file in which i am uploading a file using ajax file upload method. For backend handling of file i made a contoller in spring. But i could not find that how can i handle file in spring 2.5 in this condition ? My Code is -

我有一個jsp文件,我使用ajax文件上傳方法上傳文件。對於文件的后端處理我在春天制作了一個控制器。但我無法找到在這種情況下如何處理彈簧2.5中的文件?我的代碼是 -

JSP FILE

function saveMedia() {

var formData = new FormData();

formData.append('file', $('input[type=file]')[0].files[0]);

console.log("form data " + formData);

$.ajax({

url : 'ajaxSaveMedia.do',

data : formData,

processData : false,

contentType : false,

type : 'POST',

success : function(data) {

alert(data);

},

error : function(err) {

alert(err);

}

});

}

1 个解决方案

#1

4

There are two main steps:

主要有兩個步驟:

1) add an instance of multipart resolver to the Spring context

1)將一個多部分解析器的實例添加到Spring上下文中

class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

2) add a handler method

2)添加一個處理程序方法

// I assume that your controller is annotated with /ajaxSaveMedia.do

@RequestMapping(method = RequestMethod.POST)

public @ResponseBody String doUpload(@RequestParam("file") MultipartFile multipartFile) {

return "Uploaded: " + multipartFile.getSize() + " bytes";

}

要從org.springframework.web.multipart.MultipartFile獲取java.io.File的實例:

File file = new File("my-file.txt");

multipartFile.transferTo(file);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值