首页 > Web开发 > 详细

文件上传

时间:2015-03-25 18:44:39      阅读:206      评论:0      收藏:0      [点我收藏+]

fileupload服务控件上传

int leng = FileUpload1.PostedFile.ContentLength;//获取文件大小

            string fileName = FileUpload1.PostedFile.FileName;//获取文件名
            if (!string.IsNullOrEmpty(fileName))
            {
                string savePath = Server.MapPath("/image/");//上传的位置
                string extName = System.IO.Path.GetExtension(fileName);//获取扩展名
                string newName = Guid.NewGuid().ToString();//新文件名
                string text = savePath + newName + extName;//路径+文件名+扩展名

                FileUpload1.PostedFile.SaveAs(text);//保存
                Response.Write("<script>alert(‘长传成功" + leng/1024000 + "‘)</script>");

            }
            else
            {
                Response.Write("<script>alert(‘未选择上传文件‘)</script>");
            }

input=“file”

 1   if (Request.HttpMethod.ToLower() == "post")//form表单添加enctype="multipart/form-data"
 2             {
 3                 HttpFileCollection files = Request.Files;//文件集合
 4                 HttpPostedFile file = files[0];//第一个文件
 5                 if (file!=null)
 6                 {
 7                     string fileName = file.FileName;//获取文件名
 8                     string ExtName = Path.GetExtension(fileName);//获取扩展名
 9                     string savePath = Server.MapPath("/image/");//保存的路径
10                     string text = savePath+ fileName + ExtName;
11                     file.SaveAs(text);//保存
12
 using (Image oldImg = Image.FromStream(file.InputStream))//缩略图
                    {
                        using (Image thumImg = new Bitmap(100, 100))
                        {
                            using (Graphics g = Graphics.FromImage(thumImg))
                            {
                                g.DrawImage(oldImg //要画的图片
                                    , new Rectangle(0, 0, 100, 100)  //表示要画到缩略图的哪个位置(画满整个缩略图)
                                    , new Rectangle(0, 0, oldImg.Width, oldImg.Height) //将原图整个画到缩略图
                                    , GraphicsUnit.Pixel); //指定单位是像素
                            }

                            //将缩略图保存到服务器的磁盘
                            //将缩略图保存到 image\
                            string thumphyPath = Server.MapPath("/image/");
                            string thumFullPath = thumphyPath + newfile; //获取缩略图的完整路径
                            thumImg.Save(thumFullPath);
                        }
                    }

 

13                 }
14             }

文件大小限制

 <system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="1048576000" /><!--最大上传1G文件,--> </requestFiltering> </security> </system.webServer> 

 

文件上传

原文:http://www.cnblogs.com/junhuang/p/4366136.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!