-1

I want to give user the option to change their profile picture.On hitting the save button my controller action is hit but I get null value for HttpPostedFileBase.I am uploading files first time in mvc so not able to figure out where I am doing wrong and hence getting null value in controller.

controller

  [AuthenticationRequired]
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult ChangeProfilePicture(HttpPostedFileBase imageData)
    {
    }

view

    @using (Ajax.BeginForm("ChangeProfilePicture", "Account", new { enctype = "multipart/form-data" },new AjaxOptions { OnSuccess = "OnSuccess" }))
    {
        @Html.AntiForgeryToken()

        <div class="modal-body" id="tilesDescription">
            <div class="row">
                <div class="col-md-12">
                    <div class="text-center">
                        <div class="fileUpload btn btn-primary">
                            <span>Select a photo from your computer</span>
                            <input id="uploadBtn" type="file" class="upload" name="imageData" accept="image/*" />
                        </div>
                        <div class="text-center">
                            <img id="imgprvw" alt="uploaded image preview" class="imgPreview hide" />
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="modal-footer">
            <button type="button" class="btn btn-rounded btn-sm btn-tiles" data-dismiss="modal">Cancel</button>
            <button type="submit" class="btn btn-rounded btn-sm btn-tiles disabled" id="btnProfilePic">Set as profile picture</button>
        </div>
    }
rupinder18
  • 795
  • 1
  • 18
  • 43
  • You cannot post file inputs using `Ajax.BeginForm()` You can use `FormData` as shown in [this answer](http://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc/29293681#29293681) –  Jul 29 '15 at 10:12

1 Answers1

0

You cannot post your file using ajax like that, cause is not supported.

From my experience, the easiest way to get files posted using Ajax (if using jquery), is using :

http://malsup.com/jquery/form/

Daniel Conde Marin
  • 7,588
  • 4
  • 35
  • 44