Asp.net MVC:上传多个图像文件? [英] Asp.net MVC: upload multiple image files?

查看:181
本文介绍了Asp.net MVC:上传多个图像文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我用的文件上传在ajaxtoolbox在网络表单前,喜欢它是如何工作的。是否有MVC类似的方式?或者,有没有一种现有的控制可以做到这一点呢?自由控制较好,但它是确定即使它的成本约$。

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 $.

感谢

推荐答案

使用此 jQuery插件

仅仅包含插件的js文件,创建标记:

just include plugin js files, create tag:

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

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

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

添加一些JavaScript代码:

Add some JavaScript:

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

在控制器动作只是检查Request.Files,做任何你想要的。
这里是一个很好的文档

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

这篇关于Asp.net MVC:上传多个图像文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆