【问题标题】:MVC upload and save form image with AjaxMVC 使用 Ajax 上传和保存表单图像
【发布时间】:2014-04-10 12:26:11
【问题描述】:

我有一个包含 3 个输入(文本、图像、提交按钮)的表单。

@using (Html.BeginForm("Save", "User", FormMethod.Post, new {Id="Form1", enctype = "multipart/form-data"}))
        {
        <input id="FileUploadInput" name="Image" type="file"/>
        <input id="FirstName"  Name="FirstName">
        <input type="submit" id="inputSubmit" value="Save" />
        }

现在我想用 AJAX 从 javascript 提交这个表单

$("#inputSubmit").click(function (e) {
            e.preventDefault();
            var form = $("#Form1");
            form.validate();
            if (form.valid()) {
                $.ajax({
                    url: "/User/Save",
                    data: form.serialize(),
                    type: "POST",
                    success: function (data) {
                        if (data === "") {
                            location.reload();
                        }
                        else {
                            $(".content").html(data);
                            $.validator.unobtrusive.parse($(".content"));
                        }
                    }
                });
            }

            return false;
        });

在我的控制器文件中。

public ActionResult Save(UserProfileSettings settings)
{
  var image = setings.Image
  var name = settings.Firstname
}

我的模型

public class UserProfileSettings
{
   public string FirstName { get; set; }
   public HttpPostedFileBase Image { get; set; }
}

问题在于,在我的控制器方法中,我得到了 settins.FirstName,但 settings.Image 始终为空。我认为,使用这种方法无法序列化图像文件。

【问题讨论】:

标签: javascript jquery ajax asp.net-mvc image


【解决方案1】:

尝试使用jquery插件多次上传: http://blueimp.github.io/jQuery-File-Upload/

【讨论】:

    【解决方案2】:

    正如之前的 Darin Dimitrov suggested,最好使用 jquery forms plugin。我已经在我的另一个答案here 中发布了这个。

    快速示例

    查看

    @using (Ajax.BeginForm("YourAction", "YourController", new AjaxOptions() { HttpMethod = "POST" }, new { enctype = "multipart/form-data"}))
    {
        @Html.AntiForgeryToken()
        <input type="file" name="files"><br>
        <input type="submit" value="Upload File to Server">
    }
    

    控制器

    [HttpPost]
    [ValidateAntiForgeryToken]
    public void YourAction(IEnumerable<HttpPostedFileBase> files)
    {
        if (files != null)
        {
            foreach (var file in files)
            {
                // Verify that the user selected a file
                if (file != null && file.ContentLength > 0)
                {
                    // extract only the fielname
                    var fileName = Path.GetFileName(file.FileName);
                    // TODO: need to define destination
                    var path = Path.Combine(Server.MapPath("~/Upload"), fileName);
                    file.SaveAs(path);
                }
            }
        }
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 2022-01-24
    • 2011-05-07
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多