c# - 使用 MVC3 以相同的形式上传多个图像

标签 c# asp.net-mvc-3 file-upload

这是我的 Controller 代码和我的 View :

@using (Html.BeginForm("Create", "Product", FormMethod.Post, new { enctype = "multipart/form-data" })) {
    @Html.ValidationSummary()

    <div class="form-field">
        <p>@Html.LabelFor(m => m.Name)</p>
        @Html.EditorFor(m => m.Name)
    </div>

    <div class="form-field">
        <p>@Html.LabelFor(m => m.Description)</p>
        @Html.EditorFor(m => m.Description)
    </div>

    <div class="form-field">
        <p>@Html.LabelFor(m => m.UnitPrice)</p>
        @Html.EditorFor(m => m.UnitPrice)
    </div>

    <div class="form-field">
        <input type="file" name="image1" />
        <input type="file" name="image2" />
        <input type="file" name="image3" />
    </div>

    <div class="form-field">
        <input type="submit" value="Create" />
    </div>
}

在 Controller 中。不关注action方法的内容,只关注List<HttpPostedFileBase>类型的参数.这是正确的做事方式吗?事实上,images提交表单后为空。

[HttpPost]
public ActionResult Create(ProductModel model, List<HttpPostedFileBase> images)
{
    try
    {
        if (ModelState.IsValid)
        {
            var newProduct = Mapper.Map<ProductModel, Product>(model);
            _productRepository.CreateProduct(newProduct);
            _productRepository.SaveChanges();
        }
        return RedirectToAction("Index");
    }
    catch
    {
        return View(model);
    }
}

如果您能提供一个非常简单的示例,那就太棒了。

最佳答案

好的,这是一个简单的例子来说明如何做。最终结果:

enter image description here

HTML 标记是一个简单的表单,带有一个提交按钮。

@using (Html.BeginForm("Create", "Product", FormMethod.Post, new { enctype = "multipart/form-data" })) {
    @Html.ValidationSummary()

    <div class="form-field">
        <p>Select pictures:</p>
        <div class="upload-container">
            <div class="upload">
                <input type="file" name="files" id="file1" /> 
                <img src="@Url.Content("~/Public/images/delete.png")" alt="Remove picture." />
            </div>
        </div>        
    </div>

    <div class="form-field">
        <input type="submit" value="Create" />
    </div>
}

我们还需要一些 jQuery 魔法,以便每次有人添加图像时,我们也让他们根据需要添加更多图像。一个用户可以上传 N 张图片。

<script type="text/javascript">
    $(document).ready(function () {
        var currentImage = 1;
        $("body").on("change", "input[name='files']", function () {
            var pathToRemoveIcon = "@Url.Content("~/Public/images/delete.png")";
            currentImage = currentImage + 1;
            var htmlToAppend = '<div class="upload"><input type="file" name="files" id="file' + currentImage + '" /><img src="' + pathToRemoveIcon + '" alt="Remove picture." /></div>';
            $('.upload-container').append(htmlToAppend);
        }).on("click", ".upload img", function () {
            if ($(this).parent().siblings().length > 0) {
                $(this).parent().remove();    
            }
        });
    });
</script>

最后是 Controller 代码:

[HttpPost]
public ActionResult Create(IEnumerable<HttpPostedFileBase> files)
{
    try
    {
        if (ModelState.IsValid)
        {
            //Persist the files uploaded.
        }
        return RedirectToAction("Index");
    }
    catch
    {
        return View(model);
    }
}

关于c# - 使用 MVC3 以相同的形式上传多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8845012/

相关文章:

windows - 无法通过 psftp 使用通配符上传所有文件

java - 上传时如何使用多路复用http2功能

c# - unity 在最小和最大距离之间旋转

c# - 通过 ActionFilterAttribute mvc3.net 处理多个表单提交

c# - 是否可以*仅**跨子域共享表单例份验证?

asp.net-mvc-3 - System.ComponentModel.DataAnnotations 备忘单?

jquery - ASP.NET MVC 3 Razor jQuery 完整日历 JSON 日期时间格式

Node.js 文件上传未初始化的解析器

c# - .Net 在哪里存储泛型类型的静态字段的值?

c# - 对象类型 System.Data.DataRowView 不存在映射?