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