2020-08-26

ASP.NET webform的FileUpload控件的多图片上传到服务器

前言:
单图片上传不在概述,多图片上传的主要一点就是设置FileUpload的multiple=“multiple”,即可。
话不多说直接上代码
第一步直接拖控件,如下图所示
设置我i可操作多图片

后台代码   PYth是上传的服务端路径,filename上传的文件夹名称,这个是我自己定义的
你们可以用的我==标记的部分,包括截取文件名,文件格式
 public bool Upload(string pyth,string filename)
        {
          
           == System.IO.DirectoryInfo DirInfo = new DirectoryInfo(pyth);==
           == DirInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;==
           == HttpFileCollection upFiles = Request.Files;==
            if (upFiles.Count > 0)
            {
                int count = 0;
                int iPos;
                int iFlag = -1;
                string strExtType;
                int imgcode=0;//图片的code值
                 string imagename = "";
                string realimageame = "";//图片名字
                string Lithologicbatchnumber = Guid.NewGuid().ToString();//图片批次编号
                List<Totallithology> Totallithologylist = new List<Totallithology>();
                     
                ==for (int i = 0; i < upFiles.Count; i++)==
                {
                    string extendName = string.Empty;
                    string datestr = DateTime.Now.Ticks.ToString() + i;
                    HttpPostedFile postedFile = upFiles[i];
                    if (postedFile.ContentLength > 0)
                    {
                       ==string imageame = postedFile.FileName;==
                        iPos = imageame.LastIndexOf('.');
                        if (iPos >= 0)
                        {
                            //.txt  .doc .ppt .pdf .html .xls .xlsx
                            strExtType = imageame.Substring(iPos);
                        }                   
                        #region  老的处理方式
                        iFlag = imageame.LastIndexOf("\\");
                        if (iFlag >= 0)
                        {
                           == imagename = imageame.Substring(iFlag + 1);==
                            string rty = imagename.Substring(1, 4);
                           == realimageame = imagename.Replace(".gif", "");==
                             ==imgcode =int.Parse(rty.Replace("0", ""));==
                        }
                        #endregion
                       
                        extendName = Path.GetExtension(postedFile.FileName.ToLower().Replace(".", ""));

                       == string savePath = pyth + imagename;//上传图片路径==

                        ==postedFile.SaveAs(savePath);==
                        count = count + 1;
                        System.Drawing.Image img = System.Drawing.Image.FromFile(savePath);
                        if (img.Width <= 100 && img.Height <= 100)
                        {
                            FileInfo fi = new FileInfo(savePath);
                            FileStream fs = fi.OpenRead();
                            byte[] picBytes = new byte[fs.Length];
                            fs.Read(picBytes, 0, Convert.ToInt32(fs.Length));
                            Totallithology single = new Totallithology();
                            single.Lithologyfilename = filename;
                            single.Lithologicbatchnumber = Lithologicbatchnumber;
                            single.LithologyCode =imgcode;
                            single.LithologyImagename =realimageame;
                            single.LithologyImage = picBytes;

                            Totallithologylist.Add(single);
                        }                         
                    }
                }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值