How to consume image upload data as byte[] using Spring MVC 3 -
i need write image data in particular directory on server side getting null raw byte[] image upload data trying send html form , jquery ajaxuploader plugin here.
following snippet controller using handle raw bytes of image being uploaded:
@requestmapping(value = "uploadimage", method = requestmethod.post) public void uploadimage(byte[] uploaddata, writer writer, httpservletrequest request) throws ioexception, jsonexception { //uploaddata turning out null //.. } @initbinder protected void initbinder(servletrequestdatabinder binder) { binder.registercustomeditor(byte[].class, new bytearraymultipartfileeditor()); } i have got following configured in spring configuration file handling uploads:
<bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver"> </bean> i using spring mvc 3. guide me on how send raw bytes of upload data?
thanks.
first, if you're form uploading image, make sure content type "multipart/form-data". might want change requestmapping follows:
@requestmapping(value = "uploadimage", method = requestmethod.post, headers={"content-type=multipart/form-data"}) also, i'd suggest using commonsmultipartfile handle upload. change function signature follows, "fieldname" name of input field in form:
public void uploadimage(byte[] uploaddata, writer writer, httpservletrequest request, @requestparam("fieldname") commonsmultipartfile file) then can raw bytes follows:
file.getbytes() make sure include commons-fileupload dependency commonsmultipartfile.
i'm using spring3 + jquery ajaxform , works charm. hope helps!
Comments
Post a Comment