用input标签 文件,多文件上传

mac2022-06-30  22

单个文件,多个文件区别不大,只是需要把多个文件装在一个容器里面,循环遍历即可;

需要注意的 input 标签中name属性,一定要指定;  在这是  fileBase 

需要确定method必须是post ; enctype必须指定为multipart/form-data

单文件

HTML  ----  Using

@using (Html.BeginForm("Load", "UPLoad", FormMethod.Post, new { enctype = "multipart/form-data" })) { <p> <input type="file" name="fileBase" value="fileBase"/> <input type="submit" name="name" value="提交" /> </p> }

HTML  ----  form

<form action="/UPLoad/Load" method="post" enctype="multipart/form-data"> <p> <input type="file" name="fileBase" value="fileBase" /> <input type="submit" name="name" value="提交" /> </p> </form>

 这两种表单,看个人需要自行选用,

控制器

public ActionResult Load(HttpPostedFileBase fileBase) { //判断是否获取文件 if (fileBase != null) { var s = fileBase.FileName; //存储文件夹路径 var sks = "/NewFold/"; //判断是否存在路径 if (!Directory.Exists(Server.MapPath(sks))) //不存在 建一个 Directory.CreateDirectory(Server.MapPath(sks)); fileBase.SaveAs(Server.MapPath(sks + s)); } return View(); } 

多文件

HTML---using

只是在input 标签中加  multiple 属性  就是下面这样:          form 标签中也是这样 

 

@using (Html.BeginForm("Load", "UPLoad", FormMethod.Post, new { enctype = "multipart/form-data" })) { <p> <input type="file" name="fileBase" value="fileBase" multiple /> <input type="submit" name="name" value="提交" /> </p> }

 

控制器

public ActionResult Load(IEnumerable<HttpPostedFileBase> fileBase) { if (fileBase != null) { foreach (var item in fileBase) { var s = item.FileName; var sks = "/NewFold/"; if (!Directory.Exists(Server.MapPath(sks))) Directory.CreateDirectory(Server.MapPath(sks)); item.SaveAs(Server.MapPath(sks + s)); } } return View(); }

 

可以试试!

 

转载于:https://www.cnblogs.com/Ghajini-x/p/10721938.html

相关资源:如何点击a标签实现弹出input file上传文件对话框
最新回复(0)