0

I am getting 'null' value for HttpPostedBaseFile in controller.I am calling the Action method using jquery through Json.

View code

@using (Html.BeginForm("SaveAlertList", "AlertListUpload", new { enctype = "multipart/form-data" }, FormMethod.Post, new { id = "formUpload" }))
{
    @Html.ValidationSummary(null, new {@class = "validation-summary formErrors ui-state-error invisible"})

    <div class="table-container">
        <div class="table-container-row">
            <div class="table-container-cell">
                <fieldset>
                    <legend id="importlegend"><b>Import</b></legend>
                    <input type="file" id="uploadfile" name="uploadfile" style="width: 370px; height: 22px" />
                </fieldset>
            </div>
        </div>
    </div>

    <div id="AlertListDetails">
        <table id="grdAlertListUpload" style="border-collapse: separate"></table>
    </div>
}

Controller Code

[HttpPost]
    public JsonResult SaveAlertList(HttpPostedFileBase uploadfile)
    {
        string message1 = string.Empty;

        HttpPostedFileBase file = Request.Files["uploadfile"];
        try
        {
            if (uploadfile != null && uploadfile.ContentLength != 0)
            {
                var fileName = Path.GetFileName(uploadfile.FileName);

                if (fileName !=null)
                {
                    var path = Path.Combine(Server.MapPath(UploadFolderPath), fileName);
                    uploadfile.SaveAs(path);
                }

                message1 = ConfirmationMessages.AlertListUploadComplete;
            }
            return Json(new { Result = "OK", Message1 = message1 });
        }
        catch(Exception ex)
        {
            LoggerService.Logger.Log(ex);
            return Json(new { Result = "ERROR", Message1 = ex.Message });
        }
    }

Here is the jquery code

$("#btnUpload").on('click', function () {
  loadingDialog.show("Saving data..."); 
  performJsonCall(AlertListUpload.SaveAlertList, 'json', "", function(data) { 
    AlertListUpload.SaveSuccess(data);
  });
});

Any help is appreciated.

Channakeshav
  • 95
  • 1
  • 6
  • Your _calling the method through jquery_ - don't you think that code might be important! Are your useing `FormData`? –  Apr 01 '15 at 21:01
  • Yes am calling the method using jquery, here is the code $("#btnUpload").on('click', function () { loadingDialog.show("Saving data..."); performJsonCall(AlertListUpload.SaveAlertList, 'json', "", function(data) { AlertListUpload.SaveSuccess(data); } ); }); – Channakeshav Apr 01 '15 at 21:26
  • I am not using FormData – Channakeshav Apr 01 '15 at 21:35
  • Edit you question with the code (not add it to comments) and there is nothing in your comment which shows the ajax call anyway. In any case you need to use `FormData` to upload a file using ajax - [refer this answer](http://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc/29293681#29293681) –  Apr 01 '15 at 21:35
  • Stephen, Is it supported in IE? – Channakeshav Apr 01 '15 at 23:29
  • `FormData` in IE is only supported from IE10. Also you should read [this](https://developer.mozilla.org/en-US/docs/Web/Guide/Using_FormData_Objects) and the link at the bottom re: _uploading files via AJAX without FormData_ –  Apr 01 '15 at 23:33
  • How can I upload files asynchronously?http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously – Nathan Smith Apr 01 '15 at 23:49

0 Answers0