【问题标题】:Trying to POST image file - parameter in ActionResult is null [duplicate]尝试发布图像文件 - ActionResult 中的参数为空 [重复]
【发布时间】:2015-01-15 14:28:09
【问题描述】:

我想使用 MVC 和 jQuery 进行文件上传,但我的参数为空。在我的 jQuery 中,我获取表单数据并为其分配一个变量,然后将其发布到 ActionResult。我的参数为空,我不确定我是否正确传递数据。

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
    });
});

操作结果

 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();
        }

【问题讨论】:

  • @Rory McCrossan - 我的帖子是关于 ASP.NET MVC 的,这个答案只显示客户端代码。
  • 没错。这里的问题是由于您的客户端代码,解决方案与我链接到的问题相同。

标签: jquery asp.net-mvc-5 image-uploading


【解决方案1】:

您的 UploadImage 需要一个文件 POST 参数。您在前端代码的哪个位置指定?看不到你经过它的地方。

尝试将 HttpPostedFileBase 文件更改为 HttpPostedFileBase userImage

【讨论】:

  • 我注意到我实际上并没有传递数据,但它仍然为空。我只需要知道我的参数在 C# 中是什么数据类型。我用新的 javaScript 更新了我的 SO 帖子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-25
相关资源
最近更新 更多