[英]File upload using ajax and spring
我正在尝试上传文件并在服务器端读取它。 但我无法读取文件,而是出现异常
Required MultipartFile parameter 'file' is not present
下面是相同的代码片段。 如果我在这里做错了什么,你能告诉我吗? 有没有其他方法可以在服务器端读取ajax请求发送的文件。
<form id="dealform" method="post" enctype="multipart/form-data" type="file">
<input type="file" name="file" id="upload_file" style="visibility: hidden;width:0px;height:0px;"/><input id="fg-upload-button" type="submit" value="Upload" style="display:none;"/>
</form>
this.getRecord = function(params) {
var file = $('#upload_file').prop("files")[0];
$.ajax({
url : /Upload,
data : file,
type : 'POST',
dataType : 'json',
timeout : json_timeout,
error : function(){
that.notifyGetDataError('error getting:');
},
success : function(data){
that.notifyGetDataSuccess();
}
});
};
In the controller :
@RequestMapping(value = "/Upload.json", method = RequestMethod.POST)
public ModelAndView getContents(@RequestParam("file") MultipartFile file) {
}
Using the below in applicationContext.xml
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
该代码将文件本身作为有效负载传递给您的服务器。 但是,您的控制器期望文件作为参数“file”的值发送
您将文件作为 dataType="json" 发送,这可能会导致您出现问题,因为您的内容类型是 multipart/form-data
您的问题的另一个链接是here
您可以点击此链接,其中包含MultipartFile的控制器代码
data : {file1:file}
并在控制器中
getContents(@RequestParam("file1") MultipartFile file)
在控制器方法中,只需将请求参数的名称更改为数据:
@RequestMapping(value = "/Upload.json", method = RequestMethod.POST)
public ModelAndView getContents(@RequestParam("data") MultipartFile file) {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.