Java 使用ajax和spring上传文件
我试图上传一个文件并在服务器端读取它。但是我无法读取文件,而是遇到了一个异常Java 使用ajax和spring上传文件,java,jquery,spring,Java,Jquery,Spring,我试图上传一个文件并在服务器端读取它。但是我无法读取文件,而是遇到了一个异常 Required MultipartFile parameter 'file' is not present 下面是同样的代码片段。如果我在这里做错了什么,你能告诉我吗。有没有其他方法可以读取服务器端ajax请求发送的文件 <form id="dealform" method="post" enctype="multipart/form-data" type="file"> <input type
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>
this.getRecord=函数(参数){
var file=$('#上传文件').prop(“文件”)[0];
$.ajax({
url:/Upload,
数据:文件,
键入:“POST”,
数据类型:“json”,
超时:json_超时,
错误:函数(){
notifyGetDataError('error get:');
},
成功:功能(数据){
notifyGetDataSucces();
}
});
};
在控制器中:
@RequestMapping(value=“/Upload.json”,method=RequestMethod.POST)
publicmodelandview getContents(@RequestParam(“file”)MultipartFile文件){
}
在applicationContext.xml中使用以下命令
代码将文件本身作为有效负载传递给服务器。但是,您的控制器希望将文件作为参数“file”的值发送。您将文件作为dataType=“json”发送,这可能会导致您出现问题,因为您的内容类型是多部分/表单数据
点击这个链接
您的问题的另一个链接是
您可以遵循此链接,该链接包含控制器代码
和在控制器中
getContents(@RequestParam("file1") MultipartFile file)
在controller方法中,只需将请求参数的名称更改为data:
@RequestMapping(value = "/Upload.json", method = RequestMethod.POST)
public ModelAndView getContents(@RequestParam("data") MultipartFile file) {
}
您收到的异常情况??所需的MultipartFile参数“file”不存在请查看我的回答中的链接。您可以告诉我如何实现此功能。您可以告诉我如何在服务器端接收文件。请添加说明。
@RequestMapping(value = "/Upload.json", method = RequestMethod.POST)
public ModelAndView getContents(@RequestParam("data") MultipartFile file) {
}