.net 导入多个文件 FileUpload控件

在前端写FileUpload控件,想要多选文件,就要写属性AllowMultiple="true"。

这个方法是来自:https://www.cnblogs.com/ret00100/archive/2009/10/19/1585859.html
我把webconfig设定写死了,其实写哪里都一样。
注意:使用的时候出现循环数量不对,可能是由于此界面由多个文件上传控件,于是
var fileList = files.GetMultiple("fupd");是为了只取fupd这个控件中选择的文件们~
----------------------------202107更新,再次使用多文件上传,再次记录一下
1 private void UploadImportFile() 2 { 3 HttpFileCollection files = HttpContext.Current.Request.Files; 4 var fileList = files.GetMultiple("filename"); //filename是fileupload控件的名字 5 for (int ifile = 0; ifile < fileList.Count; ifile++) 6 { 7 HttpPostedFile postedfile = fileList[ifile]; 8 String newName = DateTime.Now.ToString("yyyyMMddHHmmssffff"); 9 SaveData(postedfile, hidtype.Value, hidtype.Value, this.HiddenID.Value, newName); //记录到数据库里 10 string filename, fileExt; 11 filename = System.IO.Path.GetFileName(postedfile.FileName); //获取文件名 12 fileExt = System.IO.Path.GetExtension(filename); //获取文件后缀 13 int MaxAllowUploadFileSize = 100000; //定义允许上传文件大小 14 string allowexts = "xls|xlsx|doc|docx|pdf|jpg|png|gif|jpeg|rar|zip|wps|et|dps"; //定义允许上传文件类型 15 Regex allowext = new Regex(allowexts); 16 if (postedfile.ContentLength < MaxAllowUploadFileSize && allowext.IsMatch(fileExt)) //检查文件大小及扩展名 17 { 18 postedfile.SaveAs(Server.MapPath("~/") + "uploadfiles\\" + newName + fileExt); //uploadfiles为与本页面同一目录,可自行修改 19 } 20 else 21 { 22 Response.Write("<script>alert('不允许上传类型" + fileExt + "。)</script>"); 23 } 24 } 25 }
public void SaveData(HttpPostedFile files, string introduce, string newfilename, string formid, string physicsname) { string uploadpath = Server.MapPath("~/") + "oafiles\\"; //保存文件 重命名 Maticsoft.Eday.Model.com_attachs model = new Maticsoft.Eday.Model.com_attachs(); String uuid = Guid.NewGuid().ToString(); model.id = uuid; if (null != HttpContext.Current.Session["userid"]) model.employeeid = HttpContext.Current.Session["userid"].ToString(); if (null != HttpContext.Current.Session["username"]) model.employeename = HttpContext.Current.Session["username"].ToString(); model.createdate = DateTime.Now; model.updatedate = DateTime.Now; long size = files.ContentLength; model.filesize = size / (1024.00 * 1024) + "MB"; String fileType = files.FileName.Substring(files.FileName.LastIndexOf('.') + 1, files.FileName.Length - files.FileName.LastIndexOf('.') - 1); if (fileType != "jpg" && fileType != "png" && fileType != "gif" && fileType != "jpeg" && fileType != "doc" && fileType != "docx" && fileType != "xls" && fileType != "xlsx" && fileType != "rar" && fileType != "zip" && fileType != "pdf" && fileType != "wps" && fileType != "et" && fileType != "dps") { MessageBox.Show(this, "请您上传符合规则的附件格式!"); return; } model.filename = files.FileName; model.physicsname = physicsname + "." + fileType; model.newfilename = newfilename + "." + fileType; model.formid = formid;//记deptID model.introduce = introduce; model.filePath = uploadpath; model.isdelete = "1"; //附件关联表保存 Maticsoft.Eday.BLL.com_attachs bll = new Maticsoft.Eday.BLL.com_attachs(); bll.Add(model); }
浙公网安备 33010602011771号