多文件上传

这是我开发过程中用涉及到的一个功能,现在备份下来。
首先是在 web.confing 中做限制上传大小配置和超时的配置, httpruntime 的节点下有 executionTimeout ,maxRequestLength两个属性。executionTimeout设置超时的时间值,默认的为90秒,如果超出这个时间,浏览器就会受到一个超时错误的通知,可以自己设置但一般不要太长啦,maxRequestLength就是设置文件的大小的,一般默认为4MB,这个太小了,我在做的过程中就是一开始没设置这个属性,测试时候传了个10MB的文件,把系统给搞死啦^_^,具体设置如下:
< httpRuntime executionTimeout = "90"maxRequestLength="20480"/>
   接下来就是动态的创建,上传文件的控件,我是用一个脚本创建的,当然也可以用其他的方法,这个我在网上找过,但感觉没这个好就用这个啦,为了减轻服务器的压力,在这做了下限制上传文件的个数,最多不能超过5个。
        var i=1
        function addFile()
        {
         if (i<6)
            {var str = '<BR> <input type="file" name="File" runat="server" style="width: 245px"/>'
            document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
            }
        else
            {
                alert(" 您一次最多只能上传5个附件!" )
            }
            i++
        }
最后就是后台的实现啦,先判断下文件的大小,别超过限制的大小,然后判断下文件的格式,然后用 Directory 的CreateDirectory()方法创建一个存放的文件夹,然后用一个for循环存就好了,具体实现如下:
#region My method of upload file
        ///<summary>
        /// Upload file method of method
        ///</summary>
        protected void UpLoadFile()
        {
            System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
            StringBuilder strmsg = new StringBuilder("");
            int ifile;
            for (ifile = 0; ifile < files.Count; ifile++)
            {
                if (files[ifile].FileName.Length > 0)
                {
                    System.Web.HttpPostedFile postedfile = files[ifile];
                    if (postedfile.ContentLength / 10240 > 10240)// 单个文件不能大于k
                    {
                        strmsg.Append(Path.GetFileName(postedfile.FileName) + "--- 不能大于k<br>" );
                        break;
                    }
                    string fex = Path.GetExtension(postedfile.FileName);
                    if (fex != ".jpg" && fex != ".JPG" && fex != ".gif" && fex != ".GIF" && fex != ".doc" && fex != ".xls" && fex != ".txt")
                    {
                        strmsg.Append(Path.GetFileName(postedfile.FileName) + "--- 文件格式不对,只能是jpg,gif,doc,xls或txt!<br>" );
                        break;
                    }
                }
            }
 
            if (strmsg.Length <= 0)// 说明附件大小和格式都没问题
            {
                // 以下为创建附件目录
                string dirpath = Server.MapPath(" 上传附件" );
 
                if (Directory.Exists(dirpath) == false)
                {
                    Directory.CreateDirectory(dirpath);
                }
            
                for (int i = 0; i < files.Count; i++)
                {
                    System.Web.HttpPostedFile myFile = files[i];
                    string FileName = "";
                    string FileExtention = "";
                    FileName = System.IO.Path.GetFileName(myFile.FileName);
 
                    if (FileName.Length > 0)// 有文件才执行上传操作再保存数据库
                    {
                        FileExtention = System.IO.Path.GetExtension(myFile.FileName);// 得到文件得后缀名
 
                        string ppath = dirpath + @"/" + FileName ;
                        myFile.SaveAs(ppath);
 
                        ticket.IntAttachment(TBTicketNumber.Text, FileName, ppath, tbAttachment.Text);// 插入数据库
                    }
                }
 
                lbMess.Text = " 恭喜,工单创建成功!" ;
            }
            else
            {
                lbMess.Text = strmsg.ToString();
                lbMess.Visible = true;
            }
        }
         #endregion
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值