首页  ·  知识 ·  云计算
同时上传多个文件
chenguang79  http://blog.csdn.net/chenguang79/  综合  编辑:dezai  图片来源:网络
我们有时候作上传功能的时候会让用户上传多个图片或文件,一个一个上传吧有点麻烦。如果做成死的,一次上次,三个,或是五个的,有时候还不够用,这就很烦了

我们有时候作上传功能的时候会让用户上传多个图片或文件,一个一个上传吧有点麻烦。如果做成死的,一次上次,三个,或是五个的,有时候还不够用,这就很烦了。下面这种方法,在上传的地方加一个按钮,如果用户想上传几个就点几次,这样就会出现多个上传框,让他选择,好了,不多说了。下面是代码
前台代码

http://www.w3.org/1999/xhtml" >

    无标题页
   


   
   


      
   


      
       

       

       

       
       
   


这里有一点要注意的是这里面的MaxFileCounts。这个是用户上传最大数的一个限制。你可以在这里写成死值,也可以在CS进行配置,主要看你的需求。

下面是cs文件了

       这里的MaxFileCount是在我配置文件写的,你可以根据你的情况写。

       public int MaxFileCounts = MaxFileCount;     

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            HttpFileCollection fileList = HttpContext.Current.Request.Files;
            if (fileList == null)
            {
                return;
            }

            FileImage file = new FileImage();
            try
            {
                for (int i = 0; i < fileList.Count; i++)
                {
                    HttpPostedFile postedFile = fileList[i];
                    if (postedFile == null)
                        continue;

                    string fileName = Path.GetFileNameWithoutExtension(postedFile.FileName);
                    string extension = Path.GetExtension(postedFile.FileName);
                    if (string.IsNullOrEmpty(extension) == true)
                        continue;

                    bool flag = false;
                    foreach (string ext in AllowFileList)
                    {
                        if (ext == extension.ToLower())
                        {
                            flag = true;
                        }
                    }

                    if (flag == false)
                        continue;
                    string storeUrl = DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + extension.ToString();
                    string Url = storeFilePath + storeUrl;
                    string fullPath = Server.MapPath(Url);
                    postedFile.SaveAs(fullPath);
                    Hashtable ht = new Hashtable();
                    ht.Add("Title",fileName);
                    ht.Add("imgUrl",storeUrl);
                    ht.Add("imgType",postedFile.ContentType);
                    ht.Add("imgSize",postedFile.ContentLength);
                    file.insertImage(ht);//这里是我的添加语句,你可写成你自己的。

                }
            }
            catch (Exception ex)
            {
                this.Label1.Text = ex.Message;
            }
        }

本文作者:chenguang79 来源:http://blog.csdn.net/chenguang79/
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读