如何在Spring MVC中通过Ajax使用PUT方法上传文件? [英] How to upload a file using the PUT method via Ajax in Spring MVC?

查看:437
本文介绍了如何在Spring MVC中通过Ajax使用PUT方法上传文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的js代码发送一个Ajax请求到一个映射在Spring MVC中的方法的URL。

 函数更新(id)
$ $ b $ .ajax({
datatype:json,
类型:put,
url:/ wagafashion / ajax / TempAjax.htm,
data:id =+ id +& t =+ new Date().getTime(),
success:function(response)
{
alert(response);
},
error:function(e)
{
alert('Error:'+ e);
}
});





$ b

以下是简单的Spring表单,它只有一个文件浏览器和一个按钮。

 < form:form id =mainFormname =mainFormmethod =postaction =Temp .htmenctype =multipart / form-datacommandName =tempBean> 
< input type =fileid =myFilename =myFile/>
< input type =buttonid =btnSubmitname =btnSubmitonclick =update(1);值= 提交/>
<! - 当点击这个按钮时,调用js函数,提供1作为id。 - >
< / form:form>

当按下按钮时,将调用以下Spring控制器中的方法。 b

  @RequestMapping(method = {RequestMethod.PUT},value = {ajax / TempAjax})
public @ResponseBody字符串更新(HttpServletRequest请求,HttpServletResponse响应)
{
System.out.println(ServletFileUpload.isMultipartContent(request));
返回消息;





方法调用 ServletFileUpload.isMultipartContent(request)然而返回 false






我修改方法如下,

  @RequestMapping(method = {RequestMethod.PUT},value = {ajax / TempAjax } $ {$ b $ public $ {
$ {$ b $}}}通过out.println(ServletFileUpload.isMultipartContent(请求));
返回消息;





$ b js代码中的错误部分总是提示错误:[object Object] 。即使在这种情况下使用 POST 方法,也会发生同样的情况。



如何通过多部分内容Ajax(正是使用 PUT 方法)?

解决方案

真的看到这是如何将多部分文件发布到服务器?数据只包含id和一个时间。

尝试类似于:

 <$ c 









$ b $ (),
数据:$('#mainForm')。 ;
},
错误:函数(e)
{
alert('Error:'+ e);
}
});
}


I have the following js code to send an Ajax request to a URL which maps a method in Spring MVC.

function update(id)
{
    $.ajax({
        datatype:"json",
        type: "put",
        url: "/wagafashion/ajax/TempAjax.htm",
        data: "id=" + id+"&t="+new Date().getTime(),
        success: function(response)
        {
            alert(response);                        
        },
        error: function(e)
        {
            alert('Error: ' + e);
        }
    });
}

and the following is the simple Spring form that has only a file browser and a button.

<form:form id="mainForm" name="mainForm" method="post" action="Temp.htm" enctype="multipart/form-data" commandName="tempBean">
    <input type="file" id="myFile" name="myFile"/>
    <input type="button" id="btnSubmit" name="btnSubmit" onclick="update(1);" value="Submit"/>
    <!--The js function is called when this button is clicked supplying 1 as id.-->
</form:form>

The following method in Spring controller is invoked when that button is pressed.

@RequestMapping(method={RequestMethod.PUT}, value={"ajax/TempAjax"})
public @ResponseBody String update(HttpServletRequest request, HttpServletResponse response)
{
    System.out.println(ServletFileUpload.isMultipartContent(request));
    return "Message";
}

The method call ServletFileUpload.isMultipartContent(request) however returns false.


When I modify the method as follows,

@RequestMapping(method={RequestMethod.PUT}, value={"ajax/TempAjax"}, headers={"content-type=multipart/form-data"})
public @ResponseBody String update(@RequestParam MultipartFile file, HttpServletRequest request, HttpServletResponse response)
{
    System.out.println(ServletFileUpload.isMultipartContent(request));
    return "Message";
}

the error section in the js code always alerts Error: [object Object]. The same thing happens even if the POST method is used, in this case.

How to pass multipart contents via Ajax (precisely using the PUT method)?

解决方案

I can't really see how this is meant to post the multipart file to the server? The data just contains the id and a time.

Try something like:

function update(id)
{
    $.ajax({
        datatype:"json",
        type: "put",
        url: "/wagafashion/ajax/TempAjax.htm",
        data: $('#mainForm').serialize(),
        success: function(response)
        {
            alert(response);                        
        },
        error: function(e)
        {
            alert('Error: ' + e);
        }
    });
}

这篇关于如何在Spring MVC中通过Ajax使用PUT方法上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆