【问题标题】:multiple image uploads in mvc3mvc3中的多个图像上传
【发布时间】:2012-04-02 00:28:35
【问题描述】:

如果我有以下情况,我成功了。处理图像上传并存储在数据库中。记住这段代码,您将如何实现多张图片上传。 谢谢。

所以第一件事。

PropertyViewModel.cs

...
public byte[] ImageData { get; set; }
public string ImageMimeType { get; set; }

public PropertyViewModel(Property x)
{
  ....
  ImageData = x.ImageData;
  ImageMimeType = x.ImageMimeType;
}

public void ToDomainModel(Property x)
{
  ....
  x.ImageData = ImageData;
  x.ImageMimeType = ImageMimeType;
}

现在形成 Create.cshtml 剃须刀页面

 @using (Html.BeginForm("Create", "Property", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
  ...
   <input type="file" name="Image"/>
}

}

控制器处理请求

[HttpPost]
public ActionResult Create(PropertyViewModel newProperty, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                if (image != null)
                {
                    newProperty.ImageMimeType = image.ContentType;
                    newProperty.ImageData = new byte[image.ContentLength];

                    image.InputStream.Read(newProperty.ImageData, 0, image.ContentLength);
                }
                using (session...)
                {
                    using (...begin transaction)
                    {
                        MyDomain.Property model = new MyDomain.Property();
                        newProperty.ToDomainModel(model);
                        ..session save model
                        .. commiting session
                    }
                }
                return RedirectToAction("Index");
            }
            else
            {
                return View(newProperty);
            }
        }

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 image file-upload


    【解决方案1】:
    @using (Html.BeginForm("Create", "Property", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        ...
        <input type="file" name="Image"/>
        <input type="file" name="Image"/>
        <input type="file" name="Image"/>
        <input type="file" name="Image"/>
    }
    

    或者如果浏览器支持 HTML5,您可以在上传对话框中选择多个文件:

    @using (Html.BeginForm("Create", "Property", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        ...
        <input type="file" name="Image" multiple="multiple"/>
    }
    

    然后:

    public ActionResult Create(PropertyViewModel newProperty, IEnumerable<HttpPostedFileBase> image)
    

    【讨论】:

    • 谢谢你,太好了。考虑到此视图模型,您将如何实现编辑操作。根据编辑操作中的某个 id 加载所有图像。谢谢
    • 我会在视图模型中直接添加一个IEnumerable&lt;HttpPostedFileBase&gt; Image { get; set; } 属性并去掉第二个动作参数。
    • 那么我是否需要更改域属性。现在它的--------- public virtual byte[] ImageData { get { return _ImageData; } 设置 { if (_ImageData == value) 返回; _ImageData = 值; } } 公共虚拟字符串 ImageMimeType { 获取 { 返回 _ImageMimeType; } 设置 { if (_ImageMimeType == value) 返回; _ImageMimeType = 值; } }
    猜你喜欢
    • 1970-01-01
    • 2012-06-21
    • 2017-05-05
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    相关资源
    最近更新 更多