1

我正在开发一个基于 jquery 表单上传图像的组件和一个 spring mvc 控制器,该控制器生成一个 xml 对象,其中包含服务器上新图像的 url。我在客户端的 jquery 代码是:

$('#uploadForm').ajaxForm({
beforeSubmit : function(a, f, o) {
    $('#uploadOutput').html('Uploading...');
},
success : function(response) {
    var $out = $('#uploadOutput');
    var url = $(response, 'url').text();
    $out.html('<img src="'+url+'4" alt="'+url+'"/>');
},
dataType : "xml"});

我的表格是:

<form id="uploadForm" action="http://localhost:8080/gossipdressrest/rest/imageupload/1" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" /> 
<input type="file" name="file" />
<input type="submit" value="Submit" />

我的 Spring MVC 控制器是:

@RequestMapping(value = "/rest/imageupload/{personId}", method = RequestMethod.POST)
public @ResponseBody
ImageUrl save(@PathVariable("personId") Long personId,
        @RequestParam("file") MultipartFile file) {
    try {
        ImagePk pk = imageManager.storeTmpImage(personId, file.getBytes());
        ImageUrl imageUrl = new ImageUrl();
        imageUrl.setUrl(imageUrlResolver.getUrl(pk));
        return imageUrl;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RuntimeException(e);
    }
}

imageUrl 是一个 POJO,具有字符串类型的单个属性“url”。包含上传图片的 URL。上面的代码在 Firefox 和 chrome 中可以正常工作,但在 IE8 中会导致它向服务器发出两个请求:

第一个似乎是正确的,并且与 firefox 和 chrome 生成的相同。但它会生成另一个导致错误 405 的 GET 请求。

请求 1:POST/HTTP/1.1 gossipdressrest/rest/imageupload/1

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application / xaml + xml
Transfer-Encoding: chunked
Date: Thu, April 28, 2011 19:56:17 GMT

9e
<? xml version = "1.0" encoding = "UTF-8" standalone = "yes"?> <ImageUrl> <url> http://localhost:8080/gossipdressrest/image/1/TMP/-7884109442646822710/ </ url > </ ImageUrl>
0

请求 2:GET /gossipdressrest/rest/imageupload/1 HTTP/1.1

HTTP/1.1 405 M�todo No Permitido 
Server: Apache-Coyote/1.1
Allow: POST
Content-Type: text/html;charset=utf-8
Content-Length: 1097
Date: Thu, 28 Apr 2011 19:56:17 GMT

<html><head><title>Apache Tomcat/6.0.29 - Informe de Error</title>...

任何想法 ;-)

4

2 回答 2

2

有点晚了,但也许反应可以为其他人服务。

这可能是因为您的 mime 类型设置为application/x-ms-application或类似的东西。

例如,您只需将其设置为您的需要application/json

如果您使用的是 Spring 3.1 或更高版本,您的 ResquestMapping 应该如下所示:

@RequestMapping(value = "/rest/imageupload/{personId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)

在 Spring 3.1 以下,您必须“手动”在响应中设置 mime 类型。

于 2012-10-11T14:12:43.493 回答
0

如果你改变method = {RequestMethod.GET, RequestMethod.POST}它的工作原理?

于 2011-04-28T22:29:21.793 回答