我正在开发一个基于 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>...
任何想法 ;-)