spring - is this ajax call for below rest Service is correct or not? -


sample rest service below:

@requestmapping(value = "/image/upload", method = requestmethod.post) public void uploadimage(@requestparam("image") multipartfile fileobj)         throws exception  {   system.out.print("file name:"+fileobj.getoriginalfilename());   } 

and wrote ajax code : , accept application format json when call 400 error

            $('#user_click').click(function(){     var data = {                image:$("#file_1").val                 };     $.ajax({         url : "http://localhost:8080/myproject/image/upload",         type : "post",         contenttype : false,         crossdomain : true,         data : json.stringify(data),         datatype : 'json',         async : true,         success : function(result) {             alert('the selected items uploaded');         },         error: function(message){           alert("error:"+json.stringify(message));           }        }); 

is ajax code correct or not?

no, not work since ajax request not transfer file data.

the solutions

  1. use file upload plugin jquery-form

ex:

$('#myform').ajaxsubmit({     url : 'http://localhost:8080/myproject/image/upload',     type : "post",     datatype : "json",     success : function(response) {      },     error : function(response) {      } }); 
  1. use html5 formdata (sadly no ie support)

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

python - cx_oracle unable to find Oracle Client -