0

I am trying to upload file in my application.

For that in my view I have

<div>
    <input id="Excelfile_id" type="file" value="Insert" class="import1" />
</div>
<div>
    <input type="button" id="excelbutton_id" value="import" class="import2" />
</div>

Jquery function is,

$.ajax({
    type: 'POST',
    url: '@Url.Action("import")', // we are calling json method
    dataType: 'json',
    contentType: 'multipart/form-data',
    data: { db: dbConn, excelfile: $("#Excelfile_id").attr('files') },
    success: function (listofuser) {
        // region contains the JSON formatted list
        // of region passed from the controller
        if (!listofuser.empty) {

and my controller is,

public JsonResult import(HttpPostedFileBase excelfile)
{
    if (excelfile.ContentLength == 0 || excelfile == null)

So, i am getting error in controller for excelfile variable as null reference.

Could you please suggest?

Thanks

user1196985
  • 120
  • 10
  • did you include enctype="multipart/form-data" in the form meta-data ? – DinoMyte Dec 14 '15 at 23:01
  • You need to use `FormData`. Refer [this answer](http://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc/29293681#29293681) fro an example –  Dec 14 '15 at 23:07
  • DinoMyte, i did not include it in form meta-data. – user1196985 Dec 14 '15 at 23:28
  • Stephen, I will take a look at that answer, thanks for suggestion – user1196985 Dec 14 '15 at 23:29
  • Hi stephen, no luck with other answer. – user1196985 Dec 14 '15 at 23:53
  • @user1196985 (see how this message starts is you want to notify someone). The method in the linked answer works so you obviously have errors in your code but we cannot guess what they are. –  Dec 14 '15 at 23:59
  • Okay, @stephen I will take a look – user1196985 Dec 15 '15 at 00:02
  • Stephen, i am also using data: Json and html.beginform() method, does it have any thing to do with my error. and as DinoMyte said, I have not included form meta-data. – user1196985 Dec 15 '15 at 00:05
  • Again, If you want to inform a user start your message with the `@...`. If you do not edit your question to show the code you are now using, how do you expect anyone to correct it! (and it has nothing to do with `form meta-data`) –  Dec 15 '15 at 00:17

0 Answers0