In this article I will explain and provide solution about why
AJAX File Upload does not work with Web Methods (Page Methods) in
ASP.Net using C# and VB.Net.
Problem
When we try to upload a
File using Form Data and
jQuery AJAX using a
Web Method (Page Method), it does not work.
Reason
By default,
ASP.Net Web Methods (Page Methods) are only allowed to handle calls for
Content Type application/json.
In contrast, File upload process requires the Content Type to be set as multipart/form-data.
Thus, it is not possible to upload a File using a
Web Method (Page Method) in
ASP.Net.
Alternative Solutions
There are two alternative solutions which will surely solve the above problem.
1. Web Service
You can create a
Web Service and then use its
Web Method for uploading Files using Form Data and
AJAX. The following article provides detailed information on uploading Files using a
Web Service in
ASP.Net.
2. Generic Handler
Another solution is to use a Generic Handler for uploading Files using Form Data and
AJAX. The following article provides detailed information on uploading Files using a Generic Handler in
ASP.Net.