2014-12-05 73 views
0

我有一个对话框,我从哪里给出一个选项来上传文件,但它不工作。我的意思是要求不去控制器。我的代码如下所示: 我的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> 

+0

你如何得到contextpath?你的web.xml配置是否正确? – 2014-12-05 15:59:35

+0

是的,我正在获取像这样的上下文路径 user07 2014-12-05 16:35:16

+0

您使用变量作为contexPath,在ajax调用中缺少t,这可能是问题吗? – 2014-12-05 16:36:40

回答

0

我认为没有被要求控制方法的原因是参数FormDataMultiPart,这是球衣文件上传API。您正在将此API与Spring MVC混合使用。

我建议你使用MultipartFile API作为上传目的,如下所示。

@RequestMapping(value= "/fileUploadForm" , method = RequestMethod.POST) 
@Consumes(MediaType.MULTIPART_FORM_DATA) 
public @ResponseBody 
String uploadFile(@RequestParam("file") MultipartFile file){ 
    // your code... 
} 
+0

所以你现在得到什么? – RE350 2014-12-08 08:09:33

+0

对不起,我无法理解你的问题,你的意思是它在对话框中的形式可能会导致问题? – RE350 2014-12-08 08:18:08

+0

你说你的Spring API是正确的,因为你已经用独立客户端测试过了吗? – RE350 2014-12-08 08:18:42

相关问题