Skip to content
Advertisement

HttpPostedfileBase is null using jQuery Ajax

I have problem with uploading file In Asp.net Mvc. First of all I should use Ajax to pass the upload file value.

In javascript I have model that I fill it, When I check it with debugger is correctly fill the object, but when I send this model to server (Controller )

The httpPostedfileBase value is Always null.

I search it on google, in some post I saw that I cant use file uploader with Ajax, but in other I saw that I can.

But I can not fix my Code.

There is my Javascript Code.

JavaScript

My Controller public virtual bool SaveOneDatabase(ResturantSharingViewModel result) My ResturantSharingViewModel View Model

JavaScript

My Html Elements

JavaScript

Advertisement

Answer

First, it’s possible to upload with Ajax, the important thing is you need to set <form enctype="multipart/form-data"></form> on you form to tell it your form has an file upload input. Then you need to accept HttpPostedFileBase as an input parameter in your controller action.

Try this. Example of jquery upload code. (Taken mostly from How can I upload files asynchronously?)

JavaScript

HTML Form needs this attribute. See this post why you need it -> What does enctype=’multipart/form-data’ mean?

JavaScript

C#

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement