【问题标题】:IE8 file upload problem with jquery form + Spring MVC controller returning XMLjquery表单+ Spring MVC控制器返回XML的IE8文件上传问题
【发布时间】:2011-04-28 21:45:31
【问题描述】:

我正在开发一个基于 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>...

任何想法;-)

【问题讨论】:

    标签: xml file-upload spring-mvc internet-explorer-8 jquery-forms-plugin


    【解决方案1】:

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

    这可能是因为您的 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 类型。

    【讨论】:

      【解决方案2】:

      如果你改成method = {RequestMethod.GET, RequestMethod.POST} 可以吗?

      【讨论】:

      • 将 POST 选项保留在 Controller 中,并在 localhost 之外使用 IE6 到 IE8 攻击开发服务器。
      猜你喜欢
      • 2015-10-12
      • 2014-12-14
      • 2015-10-12
      • 2013-07-04
      • 1970-01-01
      • 2011-06-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多