我有一个对话框,我从哪里给出一个选项来上传文件,但它不工作。我的意思是要求不去控制器。我的代码如下所示: 我的JSP事业部内容:对话框如何上传任何文件并将其保存到一个特定的文件夹在春天mvc
内容是:
<form method="post" action="fileUploadForm" enctype="multipart/form-data">
Please select a file to upload :<input type="file" name="fileUpload" id="Uploadfile"/>
<input type="submit" value="Upload" onclick="Uploadfiles();"/>
</form>
Java脚本函数: 现在我怎么也得使用表单数据发送该请求到控制器,并再次如果成功上传,我应该获得字符串响应。我已经尝试过这样但它不工作:
<script type="text/javascript">
function Uploadfiles(){
$('#result').html('');
var formdata=document.getElementById("Uploadfile");
var fd = new FormData(formdata);
fd.append("CustomField","This is an additional data");
$.ajax({
url: contextPath +"/fileUploadForm",
dataType: 'text',
data: fd,
processData: false,
contentType: false,
type: "POST",
success: function(data){
alert("inside success ***");
$('#result').html(data);
} ,
error: function(){
alert("error has occured");
}
});
}
</script>
因为我不知道控制器代码应该是什么,因为我从一个网站得到它。如果我使用我在这里提到的东西,我得到错误,如不正确的语法我的意思是通过ajax调用控制器发送的数据是不正确的句法接受形式数据,即所以ajax调用发送什么问题和什么控制器代码期望。请让我知道什么应该是实现如何在ajax调用中发送文件请求的正确方式,以便控制器应该获得正确的数据。由于保存文件代码工作正常,如果我不会使用这个Ajax调用文件得到正确保存。 现在我的控制器代码:
private static final String KDC_FILE_UPLOAD_LOCATION_FOLDER = "/root/Desktop/Upload_Files/";
@RequestMapping(value= "/fileUploadForm",method = RequestMethod.POST)
public String handleFileUpload(HttpServletRequest request,
@RequestParam CommonsMultipartFile[] fileUpload) throws Exception {
System.out.println("finally in controller ");
if (fileUpload != null && fileUpload.length > 0) {
for (CommonsMultipartFile aFile : fileUpload){
System.out.println("Saving file: " + aFile.getOriginalFilename());
if (!aFile.getOriginalFilename().equals("")) {
aFile.transferTo(new File(FILE_UPLOAD_LOCATION_FOLDER + aFile.getOriginalFilename()));
}
}
}
// returns to the view "Result"
return "File has been Uploaded Successfully";
}
的变化在我的春天调度servlet的XML文件: 低于行我已经添加仅作使用多
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
请让我知道什么是错误的,我代码,如果我们可以使用其他方法实现相同的结果。
春季调度的servlet文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd">
<bean id="userController" class="com.controller.UserLoginController">
<property name="multipartResolver" ref="multipartResolver"></property>
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- max upload size in bytes -->
<property name="maxUploadSize" value="20971520" /> <!-- 20MB -->
<!-- max size of file in memory (in bytes) -->
<property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->
</bean>
你如何得到contextpath?你的web.xml配置是否正确? – 2014-12-05 15:59:35
是的,我正在获取像这样的上下文路径 和 –
user07
2014-12-05 16:35:16
您使用变量作为contexPath,在ajax调用中缺少t,这可能是问题吗? – 2014-12-05 16:36:40