2

I want to file upload by ajax to controller.But getting null in controller method parameter. Controller

public ActionResult Upload(HttpPostedFileBase file,int value)
{
    try
    {
        if (file.ContentLength > 0)
        {
            var fileName = Path.GetFileName(file.FileName);
            var path = Path.Combine(Server.MapPath("~/File/"), fileName);
            file.SaveAs(path);
            _manager.UploadFile(file, path.ToString());
        }
        ViewBag.Message = "Upload successful";
        return RedirectToAction("Index");
    }
    catch
    {
        ViewBag.Message = "Upload failed";
        return RedirectToAction("Index");
    }
}

In index i am using normal input file type and submit type button.everything happens normal but controller method paramerter getting null why?

Index

<label class="btn btn-block btn-primary">
    Browse &hellip;
    <input type="file" name="file" class="btn" id="uploadfile" style="display: none;">
</label>
<input type="submit" class="btn" id="submit" value="Upload">

$(document).ready(function (e) {
    $('#submit').click(function (event) {
        var formData = new FormData();
        var totalfiles = document.getElementById("uploadfile");
        for (var i = 0; i < totalfiles; i++) {
            var file = document.getElementById("uploadfile").files[i];
            formData.append("uploadfile", file);
        }
        var dam=1;
        dam = $("input:checkbox:checked").attr("value");
        alert(dam);
        alert(formData);
        $.ajax(
        {
            url: '@Url.Action("Upload","Document")',
            type: "POST",
            data: { file: formData ,value:dam},
            datatype: "multipart/form-data",
            traditional: true,
            success: function (response) {
                // $("#file").html(response);
            }
        });
    })
});
Mehedi Islam
  • 59
  • 1
  • 12
  • Possible duplicate of [how to append whole set of model to formdata and obtain it in MVC](http://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc) –  Jul 16 '16 at 07:30
  • Please note that the model-view-controller tag is for questions about the pattern. There is a specific tag for the ASP.NET-MVC implementation. And refer the dupe for the correct ajax options you need when using `FormData` –  Jul 16 '16 at 07:30
  • It doesn't work Stephen.It's getting null. – Mehedi Islam Jul 17 '16 at 06:47
  • Yes it does. If its not working for your then its because of errors in **your** code –  Jul 17 '16 at 06:49
  • Can you check what else ..please – Mehedi Islam Jul 17 '16 at 07:01
  • Check what? You have not shown the revised code you have used based on the duplicate (I'm not psychic) –  Jul 17 '16 at 07:03
  • Sorry :/...I mention earlier into my code,that's why i told you that you may check that.Now you can check my code,i have modified my code. – Mehedi Islam Jul 17 '16 at 07:18
  • And I have just rolled back your changes. You cannot change the original question and invalidate comments/answers by others. You need to append any new code you have added to the end of the question. –  Jul 17 '16 at 07:28

0 Answers0