0

I want to do a fileupload with MVC and jQuery but my paremter is null. In my jQuery I am getting the form data and assigning it a variable and then POSTING it to an ActionResult. My parameter is null and I am not sure if I am passing the data correctly.

HTML

<table>
                <tr>
                    <td><img src="@Url.Content("~/AMTImages/UserAvatar.png")" width="64" height="64" style="border: thin solid" /></td>
                    <td></td>
                    <td></td>
                    <td>
                        <form enctype="multipart/form-data" id="imageUploadForm">
                            <table>
                                <tr>
                                    <td>@Html.LabelFor(m => m.DisplayName, "Display Name: ")</td>
                                    <td style="padding-left: 10px;">@Html.TextBoxFor(m => m.DisplayName)</td>
                                </tr>
                                <tr>
                                    <td>@Html.LabelFor(m => m.UserImage, "User Image: ")</td>
                                    <td style="padding-left: 10px; float: right;">@Html.TextBoxFor(m => m.UserImage, new { type = "file", accept = "image/*" })</td>
                                </tr>
                            </table>
                        </form>
                    </td>
                    <td>
                        <table style="display:none;">
                            <tr>
                                <td>
                                    <progress></progress>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>

JavaScript

$(':file').change(function () {
    var file = this.files[0];

    $.ajax({
        url: '@Url.Action("UploadImage", "User")',  //Server script to process data
        type: 'POST',
        data: file,
        beforeSend: function() {  
        },
        success: function () {
        },
        error: function () {
        },
        processData: false,
        contentType: file.type
    });
});

ActionResult

 public ActionResult UploadImage(HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
                try
                {
                    string path = Path.Combine(Server.MapPath("~/Images"),
                                               Path.GetFileName(file.FileName));
                    file.SaveAs(path);
                    ViewBag.Message = "File uploaded successfully";
                }
                catch (Exception ex)
                {
                    ViewBag.Message = "ERROR:" + ex.Message.ToString();
                }
            else
            {
                ViewBag.Message = "You have not specified a file.";
            }

            return View();
        }
nick gowdy
  • 6,191
  • 25
  • 88
  • 157

1 Answers1

0

Your UploadImage expects a file POST param. Where do you specify that on your front-end code? Can't see where you pass it.

Try changing HttpPostedFileBase file to HttpPostedFileBase userImage

Yannis
  • 6,047
  • 5
  • 43
  • 62
  • I noticed that I was not actually passing the data but it's still null. I just need to know what data type my parameter is in C#. I updated my SO post with the new javaScript. – nick gowdy Nov 18 '14 at 12:10