mvc 利用form表单文件上传

来源:互联网 发布:js如何获取父节点 编辑:程序博客网 时间:2024/04/27 09:27

前端页面

<form action="/my/reviewdata" method="post" enctype="multipart/form-data">

<input class="imgx" name="img" type="file" multiple="multiple">

</form>



mvc action页面

public ActionResult upfile(FormCollection fc)//这个fc对象可以用来获取表单中的其他值,利用标签的name ,在这里没有用到可以不用写

{

    if ( Request.Files.Count>0)  //Request.Files 获取表单中的文件
            {
                for(int i=0;i<Request.Files.Count;i++)
                {
                    HttpPostedFileBase hpf = Request.Files[i];//这个对象是用来接收文件,利用这个对象可以获取文件的name path等
                    string path= Server.MapPath("/Upfiles/" + hpf.FileName);//拼接一个路径
                    hpf.SaveAs(path);//用SaveAs保存到上面的路径中
                }
            }

}