Asp.net MVC: upload multiple image files?

前端 未结 4 432
礼貌的吻别
礼貌的吻别 2020-12-08 05:19

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 file

4条回答
  •  渐次进展
    2020-12-08 06:05

    Use this jQuery plugin

    just include plugin js files, create tag:

    
    

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

    Add some JavaScript:

    $(function () {
        $('#fileUpload').fileupload({
            dataType: 'json',
            done: function (e, data) {
                $.each(data.result.files, function (index, file) {
                    $('

    ').text(file.name).appendTo(document.body); }); } }); });

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

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

提交回复
热议问题