multipart/form-data and asmx

For a project i was trying to pass formdata which holds input type file to be processed by asp.net web service (.asmx) . The service will upload the file to a specific folder and update the database with relevant values.

To my surprise i was not able to do that in the way i used to pass ajax data , it starts to throw some random error .

After  a deep search, i found the answer in stackoverflow,

.NET will not allow the multipart/form-data for the content-type:

which is also having a link pointing to the Scottgu’s website.

essential these are trying to prevent JSON hijacking attacks. all the asmx webmethod  unless until specified is always defaults to POST , the user has to specify explicitly the web method to use httpget to use GET method while making an ajax call.

and other important thing is


There is a built-in validation layer of protection that ASP.NET enforces for
both GET and POST based ASP.NET AJAX web methods, which is thatregardless
of the HTTP verb being used, ASP.NET always requires that the HTTP
Content-Type header is set to the value application/json. It this
content type header is not sent, ASP.NET AJAX will reject the request on the
server.
This particular thing is preventing me from posting formdata to the webmethod. Either i have to serialize the form or i have to use generic handler (.ashx) to handle file upload. Since i am not sterilizing the form because i am creating formdata on runtime , i have to settle with a generic handler to upload the file.
 
sigh… every day i am learning something.