0

I am trying to pass formdata fileupload with ajax and Jquery to my controller.

But when I am in the controller the Request.files.count is 0??

What am I been missing?

The view file

<div class="form-group">
                  @Html.LabelFor(model => model.ImageFile)

                        <!-- Example file element -->
                        <input type="file" name="FileUpload1" id="FileUpload1" />

                </div>

<script type="text/javascript">
    jQuery(document).ready(function ($) {
        $('#loginButton').click(function (e) {
            e.preventDefault();
            var formData = new FormData();
            var totalFiles = document.getElementById("FileUpload1").files.length;

            var file = document.getElementById("FileUpload1").files[0];
            formData.append("FileUpload", file);
            alert(totalFiles);
            alert(file);

        $.ajax({
            type: "POST",
            url: '/Manage/GetPartialView)',
            data: $formData,
            dataType: 'json',
            contentType: false,
            processData: false,
            success: function (result) {
                alert(result);
            },

        });
    });
  });

The controller

 [AllowAnonymous]
    [ValidateAntiForgeryToken]
    [HttpPost]
    public ActionResult GetPartialView(Listing listing)
    {
        if (Request.Files.Count > 0)
        {
            HttpPostedFileBase file = Request.Files[0];
        }


        if (ModelState.IsValid)
        {
            db.Listings.Add(listing);
            db.SaveChanges();
        }
        return PartialView("_ProfileCreateListingsPartial");
    }

Hope someone could help me with this problem /Tina

4
  • data: $formData should be data: formData (you do not have a variable named $formData). And what is the point of the Listing listing parameter - your not posting back anything relating to that model? Commented May 4, 2016 at 12:57
  • And if you are wanting to pass the model and the file, refer this answer Commented May 4, 2016 at 13:12
  • I am using a begin form so I am getting the listing model Commented May 4, 2016 at 13:48
  • You are not getting the model because your .click() even is cancelling the action and you only submitting the file :) Commented May 4, 2016 at 21:49

1 Answer 1

2

Use This:

   jQuery(document).ready(function ($) {
    $('#loginButton').click(function (e) {
        e.preventDefault();
        var formData = new FormData();
        var totalFiles = document.getElementById("FileUpload1").files.length;

        var file = document.getElementById("FileUpload1").files[0];
        formData.append("FileUpload", file);
        alert(totalFiles);
        alert(file);

    $.ajax({
        type: "POST",
        url: '/Manage/GetPartialView',
        data: formData,
        dataType: 'json',
        contentType: false,
        processData: false,
        success: function (result) {
            alert(result);
        },

    });
});


});
Sign up to request clarification or add additional context in comments.

1 Comment

It is still 0 in my controller

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.