上传(upload)文件的codebehind代码
点击次数:19 次 发布日期:2008-11-26 23:30:13 作者:源代码网
|
源代码网推荐 1。把图片文件(JPG GIF PNG)上传, 源代码网推荐 2。保存到指定的路径(在web.config中设置路径,以文件的原有格式保存), 源代码网推荐 3。并自动生成指定宽度的(在web.config中设置宽度) 源代码网推荐 4。和指定格式的(在web.config中指定缩略图的格式) 源代码网推荐 5。和原图比例相同的缩略图(根据宽度和原图的宽和高计算所略图的高度) 源代码网推荐 6。可以判断是否已经存在文件 源代码网推荐 7。如果不覆盖,则给出错误 源代码网推荐 8。如果选中"覆盖原图"checkbox,则覆盖原图。 源代码网推荐 9。可以根据要求,在webform上设置1个以上的file input和相应的checkbox 源代码网推荐 10。并在文件上传完毕后,显示原图的文件名,尺寸,字节,和 源代码网推荐 11。缩略图的文件名尺寸。 源代码网推荐 12。缩略图的文件名格式:原图+"_thumb."+指定格式,如:test.jpg_thumb.gif,以便于管理。 源代码网推荐 源代码网推荐 -------------------- 源代码网推荐 public void UploadFile(object sender, System.EventArgs e) 源代码网推荐 { 源代码网推荐 源代码网推荐 string imgNameOnly, imgNameNoExt, imgExt; 源代码网推荐 string imgThumbnail; 源代码网推荐 int erroNumber = 0; 源代码网推荐 System.Drawing.Image oriImg, newImg; 源代码网推荐 string strFePicSavePath = ConfigurationSettings.AppSettings["FePicSavePath"].ToString(); 源代码网推荐 string strFePicThumbFormat = ConfigurationSettings.AppSettings["FePicThumbFormat"].ToString().ToLower(); 源代码网推荐 int intFeThumbWidth = Int32.Parse(ConfigurationSettings.AppSettings["FePicThumbWidth"]); 源代码网推荐 string fileExt; 源代码网推荐 源代码网推荐 StringBuilder picInfo = new StringBuilder(); 源代码网推荐 源代码网推荐 if(Page.IsValid) 源代码网推荐 { 源代码网推荐 源代码网推荐 源代码网推荐 for(int i = 0;i < Request.Files.Count; i++) 源代码网推荐 { 源代码网推荐 HttpPostedFile PostedFile = Request.Files; 源代码网推荐 fileExt = (System.IO.Path.GetExtension(PostedFile.FileName)).ToString().ToLower(); 源代码网推荐 源代码网推荐 imgNameOnly = System.IO.Path.GetFileName(PostedFile.FileName); 源代码网推荐 if(fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".png") 源代码网推荐 { 源代码网推荐 源代码网推荐 if(System.IO.File.Exists(strFePicSavePath + imgNameOnly) && (checkboxlistRewrite.Items.Selected == false)) 源代码网推荐 { 源代码网推荐 erroNumber = erroNumber + 1; 源代码网推荐 picInfo.Append("<b>错误:</b>文件("+ (i+1) +") " + imgNameOnly + " 已经存在,请修改文件名<br>" ); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 erroNumber = erroNumber + 1; 源代码网推荐 picInfo.Append("<b>错误:</b>文件("+ (i+1) +") " + imgNameOnly + " 扩展名 " + fileExt + " 不被许可<br>" ); 源代码网推荐 } 源代码网推荐 源代码网推荐 } 源代码网推荐 源代码网推荐 if(erroNumber > 0) 源代码网推荐 { 源代码网推荐 picInfo.Append("<font color=red>全部操作均未完成,请修改错误,再进行操作</font><br>"); 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 for(int i = 0;i < Request.Files.Count; i++) 源代码网推荐 { 源代码网推荐 源代码网推荐 HttpPostedFile PostedFile = Request.Files; 源代码网推荐 imgNameOnly = System.IO.Path.GetFileName(PostedFile.FileName); 源代码网推荐 imgNameNoExt = System.IO.Path.GetFileNameWithoutExtension(PostedFile.FileName); 源代码网推荐 imgExt = System.IO.Path.GetExtension(PostedFile.FileName).ToString().ToLower(); 源代码网推荐 源代码网推荐 源代码网推荐 oriImg = System.Drawing.Image.FromStream(PostedFile.InputStream); 源代码网推荐 newImg = oriImg.GetThumbnailImage(intFeThumbWidth, intFeThumbWidth * oriImg.Height/oriImg.Width,null,new System.IntPtr(0)); 源代码网推荐 switch(imgExt) 源代码网推荐 { 源代码网推荐 //case ".jpeg": 源代码网推荐 case ".jpg": 源代码网推荐 oriImg.Save(strFePicSavePath + imgNameOnly , System.Drawing.Imaging.ImageFormat.Jpeg); 源代码网推荐 break; 源代码网推荐 case ".gif": 源代码网推荐 oriImg.Save(strFePicSavePath + imgNameOnly , System.Drawing.Imaging.ImageFormat.Gif); 源代码网推荐 break; 源代码网推荐 case ".png": 源代码网推荐 oriImg.Save(strFePicSavePath + imgNameOnly , System.Drawing.Imaging.ImageFormat.Png); 源代码网推荐 break; 源代码网推荐 } 源代码网推荐 源代码网推荐 //oriImg.Save(ConfigurationSettings.AppSettings["FePicSavePath"] + imgNameNoExt + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); 源代码网推荐 源代码网推荐 switch(strFePicThumbFormat) 源代码网推荐 { 源代码网推荐 //jpeg format can get the smallest file size, and the png is the largest size 源代码网推荐 //case "jpeg": 源代码网推荐 case "jpg": 源代码网推荐 newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.jpg",System.Drawing.Imaging.ImageFormat.Jpeg); 源代码网推荐 imgThumbnail = imgNameOnly + "_thumb.jpg"; 源代码网推荐 break; 源代码网推荐 case "gif": 源代码网推荐 newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.gif",System.Drawing.Imaging.ImageFormat.Gif); 源代码网推荐 imgThumbnail = imgNameOnly + "_thumb.gif"; 源代码网推荐 break; 源代码网推荐 case "png": 源代码网推荐 newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.png",System.Drawing.Imaging.ImageFormat.Png); 源代码网推荐 imgThumbnail = imgNameOnly + "_thumb.png"; 源代码网推荐 break; 源代码网推荐 default: 源代码网推荐 newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.jpg",System.Drawing.Imaging.ImageFormat.Jpeg); 源代码网推荐 imgThumbnail = imgNameOnly + "_thumb.jpg"; 源代码网推荐 break; 源代码网推荐 源代码网推荐 }//switch 源代码网推荐 源代码网推荐 picInfo.Append("<b>文件 名:</b>" + imgNameOnly + " ( " + oriImg.Width + " x " + oriImg.Height + " ) " + PostedFile.ContentLength/1024 + "KB<br>"); 源代码网推荐 picInfo.Append("<b>缩略图名:</b>" + imgThumbnail + " ( " + newImg.Width + " x " + newImg.Height + " )<br><br>"); 源代码网推荐 源代码网推荐 oriImg.Dispose(); 源代码网推荐 newImg.Dispose(); 源代码网推荐 源代码网推荐 }//for 源代码网推荐 picInfo.Append("<font color=red>所有操作成功</font><br>"); 源代码网推荐 源代码网推荐 }// if erronumber = 0 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 picInfo.Append("<font color=red>有错误,请检查。操作未成功</font><br>"); 源代码网推荐 源代码网推荐 } 源代码网推荐 源代码网推荐 lblPicInfo.Text = picInfo.ToString(); 源代码网推荐 源代码网推荐 } 源代码网供稿. |
