asp.net-mvc Asp.net MVC:上传多个图像文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25772134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:14:35  来源:igfitidea点击:

Asp.net MVC: upload multiple image files?

asp.net-mvcasp.net-mvc-4file-uploadimage-uploading

提问by urlreader

is there a good example of how to upload multiple image files in asp.net mvc? I know we can use HttpPostedFileBase to upload one file. Is there a way to upload multiple files by clicking one button?

有没有一个很好的例子来说明如何在 asp.net mvc 中上传多个图像文件?我知道我们可以使用 HttpPostedFileBase 上传一个文件。有没有办法通过单击一个按钮来上传多个文件?

I used file upload in ajaxtoolbox in webform before and like how it works. Is there a similar way in MVC? or, is there a existing control that can do this well? free control is better, but it is ok even it costs some $.

我之前在 webform 中的 ajaxtoolbox 中使用文件上传,并且喜欢它的工作原理。MVC 中有类似的方法吗?或者,是否有可以很好地做到这一点的现有控件?免费控制更好,但即使花费一些美元也可以。

Thanks

谢谢

采纳答案by aleha

Use this jQuery plugin

使用这个jQuery 插件

just include plugin js files, create tag:

只需包含插件js文件,创建标签:

<input type='file' multiple id='fileUpload' name="files[]" data-url="@Url.Action("Upload","Home")" />

(Except IE9 it is not allowing select multiple files in select dialog)

(除了 IE9,它不允许在选择对话框中选择多个文件)

Add some JavaScript:

添加一些 JavaScript:

$(function () {
    $('#fileUpload').fileupload({
        dataType: 'json',
        done: function (e, data) {
            $.each(data.result.files, function (index, file) {
                $('<p/>').text(file.name).appendTo(document.body);
            });
        }
    });
});

In controller action just check Request.Files and do whatever you want. Here is a good documentation

在控制器操作中,只需检查 Request.Files 并做任何你想做的事情。这是一个很好的文档

[HttpPost]
public JsonResult Upload() 
{
    foreach (var file in Request.Files)
    {
        if(file.ContentLength > 0)
        {
            file.SaveAs(Server.MapPath("~/Upload/" + file.FileName));
        }
    }

    return Json(new { result = true });
}

回答by Felipe Oriani

You could implement an action with POSThttp verb to that receive a collection of HttpPostedFileBaseand save all files, for sample:

您可以使用POSThttp 动词实现一个动作来接收HttpPostedFileBase并保存所有文件的集合,例如:

[HttpPost]
public ActionResult Upload(IEnumerable<HttpPostedFileBase> files) 
{
    foreach (var file in files)
    {
        file.SaveAs(Server.MapPath("~/Update/" + file.FileName));
    }

    return View();
}

Alternatively, you could read Request.Filesand do the same job,

或者,你可以阅读Request.Files并做同样的工作,

[HttpPost]
public ActionResult Upload() 
{
    foreach (var file in Request.Files)
    {
        file.SaveAs(Server.MapPath("~/Update/" + file.FileName));
    }

    return View();
}

See Also

也可以看看

回答by Kadir

I'm using this one. https://www.fyneworks.com/jquery/multiple-file-upload/

我在用这个。https://www.fyneworks.com/jquery/multiple-file-upload/

<input type="file" name="file" class="multiple" /> 

[HttpPost] 
public ActionResult Upload()  
{
    if (Request.Files.Count > 0)
    {
          foreach(var file in Request.Files) {  }
    }

    return View(); 
}

回答by Moji

Some of the basic bits required for file uploads

文件上传所需的一些基本位

Notice keyword: multiplein input element AND multipartin form element

通知关键字:多个输入元件和 多部分形式元件

HTML Side

HTML端

@using (Html.BeginForm("MyUpload", "MyController", FormMethod.Post, new { enctype = "multipart/form-data" })) { 
    <input type="file" name="myFiles" multiple />
    <button class="btn btn-default">Upload</button>   
}


Controller

控制器

[HttpPost]
public ActionResult MyUpload(IEnumerable<HttpPostedFileBase> myFiles)
{
    foreach (var file in myFiles)
    {
        if (file != null && file.ContentLength > 0)
        {
            //handle files;
        }
    }
    return View();
}