I am using file upload functionality in my asp.net MVC project. It worked great until I started using some AJAX functionality on my page.
The HttpPostedFile is always NULL on Ajax page.
How can solve this issue along with calling ajax on my page?
I am using file upload functionality in my asp.net MVC project. It worked great until I started using some AJAX functionality on my page.
The HttpPostedFile is always NULL on Ajax page.
How can solve this issue along with calling ajax on my page?
Its not possible to post a file upload using ajax unless you jump through some hoops - such as posting a sub from from within an IFrame, or by using one of the Flash based solutions. See https://stackoverflow.com/questions/254831/asp-net-free-ajax-file-upload-control
Because you cannot upload files using AJAX I would recommend you the excellent jquery form plugin which allows you to ajaxify your forms and supports file uploads. Behind the scenes the plugin generates a hidden iframe to handle the upload and is completely transparent to you:
Controller:
And finally ajaxify the form:
Also notice the usage of
HttpPostedFileBase
instead ofHttpPostedFile
in the controller action. Being an abstract class this will simplify your unit tests.XHR can not file post.
Asnc file upload use iframe or some library.