Chinaunix首页 | 论坛 | 博客
  • 博客访问: 82438
  • 博文数量: 64
  • 博客积分: 1545
  • 博客等级: 上尉
  • 技术积分: 392
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-23 15:23
文章分类

全部博文(64)

文章存档

2013年(1)

2012年(63)

我的朋友

分类: 嵌入式

2012-11-03 23:28:37

用客户端控件文件上传

用客户端控件时要注意:

       首先

的属性一定要设成:enctype="multipart/form-data";

       其次(很重要奥!)name="xxxxxxx"/>一定不要忘记“name”属性。否则在后台代码中用Request.Files是取不到值得!

具体代码如下:

       前台代码:

                   
                       

                       
                   

       后台代码:

            System.Web.HttpFileCollection files = Request.Files;
            for (int fileCount = 0; fileCount < files.Count; fileCount++)
            {
                System.Web.HttpPostedFile postedfile = files[fileCount];

                string fileName = System.IO.Path.GetFileName(postedfile.FileName);
                if (!String.IsNullOrEmpty(fileName))
                {

                    string fileExtension = System.IO.Path.GetExtension(fileName);    //获取文件类型
                    postedfile.SaveAs(Server.MapPath("~/UpLoadFiles/") + fileName);//上传到服务器的路径
                }
            }

--------------------------------------------------------------------

     或者这样写:

            HttpFileCollection files = Request.Files;
            HttpPostedFile postedfile = files.Get("fileUpload");//控件name值
            string fileName = System.IO.Path.GetFileName(postedfile.FileName);
            if (!String.IsNullOrEmpty(fileName))
            {
                string fileExtension = System.IO.Path.GetExtension(fileName);    //获取文件类型
                postedfile.SaveAs(context.Server.MapPath("~/UpLoadFiles/") + fileName);//上传到服务器的路径
            }

   或者:

           HttpPostedFile postedfile = Request.Files["fileUpload"];
            string fileName = System.IO.Path.GetFileName(postedfile.FileName);
            if (postedfile!=null)
            {
                string fileExtension = System.IO.Path.GetExtension(fileName);    //获取文件类型
                postedfile.SaveAs(context.Server.MapPath("~/upload/") + fileName);//上传到服务器的路径
              
            }


阅读(249) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~