我正在开发一个组件,用于上传基于jquery表单和spring mvc控制器的图像,该控制器生成一个包含服务器上新图像url的xml对象。我在客户端的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,只有一个String类型的属性url。包含上传图像的URL。以上代码在firefox和chrome中正常工作,但在IE8中导致它向服务器发出两个请求:
第一个似乎是正确的,并且与firefox和chrome生成的相同。但是它会生成另一个GET请求,导致错误405。
请求1:POST/HTTP/1.1 gossip press/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/gossip press/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>...
知道吗;-)
有点晚了,但也许这个回应可以为其他人服务。
这可能是因为您的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类型
如果您更改为method = {RequestMethod.GET, RequestMethod.POST}
它工作吗?