Send file with ajax of jQuery to web service in C# (asmx) -
i'm using web service method:
$.ajax({ type: 'post', url: 'page.asmx/method', contenttype: 'application/json; charset=utf-8', datatype: 'json', data: '{}' });
sending json string, , works, if try append formdata content of input , passing in data value have 500 response. have do?
you need serialize data....
var data = new formdata(); var files = $("#youruploadctrlid").get(0).files; // add uploaded image content form data collection if (files.length > 0) { data.append("uploadedfile", files[0]); } // make ajax request contenttype = false, , procesdate = false var ajaxrequest = $.ajax({ type: "post", url: "/api/fileupload/uploadfile", contenttype: false, processdata: false, data: data });
and inside controller can have like
if (httpcontext.current.request.files.allkeys.any()) { // uploaded image files collection var httppostedfile = httpcontext.current.request.files["uploadedfile"]; if (httppostedfile != null) { // validate uploaded image(optional) // complete file path var filesavepath = path.combine(httpcontext.current.server.mappath("~/uploadedfiles"), httppostedfile.filename); // save uploaded file "uploadedfiles" folder httppostedfile.saveas(filesavepath); } }
hope helps...
Comments
Post a Comment