.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);
        }
复制代码

 

 

 

posted on   张不胖  阅读(283)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示