I am developing a component for uploading images based on jquery form and a spring mvc controller that produces an xml object containing the url of the new image on the server. My jquery code on the client side is:
$('#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"});
My form is:
<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" />
and my Spring MVC Controller is:
@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 is a POJO with a single attribute 'url' of type String. Containing the URL of the uploaded image. The above code works correctly in firefox and chrome, but in IE8 causes it to make two requests to the server:
The first seems correct and is identical to that generated by firefox and chrome. But it generates another GET request that results in an error 405.
Request 1: POST / HTTP/1.1 gossipdressrest/rest/imageupload/1
H开发者_C百科TTP/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
Request 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>...
Any idea ;-)
a bit a late, but perhaps the response can serve other peoples.
This is probably caused because your mime type is set to application/x-ms-application
or something like this.
You just have to set it to your need application/json
for instance.
If you're using Spring 3.1 or later, your ResquestMapping should look like this:
@RequestMapping(value = "/rest/imageupload/{personId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
Below Spring 3.1, you have to "manually" set the mime type in the response.
If you change to method = {RequestMethod.GET, RequestMethod.POST}
it works?
精彩评论