关于System.Web.HttpContext.Current.Request.Files

killu 2003-08-23 01:26:00
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
Response.Write("Upload File NUMS="+files.Count.ToString());

在没有选择文件时也为1,为什么??
...全文
283 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
panyee 2003-08-23
  • 打赏
  • 举报
回复
当然了,你有个文件输入域,它就大于0

你要判断一下
files[0].ContentLength 是否大于0
files[0]是否为null
killu 2003-08-23
  • 打赏
  • 举报
回复
自己顶
在ASP.NET中上传图片并生成缩略图的C#源码 using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.IO; using System.Drawing.Imaging; namespace eMeng.Exam { /// /// Thumbnail 的摘要说明。 /// public class Thumbnail : System.Web.UI.Page { protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.Button Button1; private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 Label1.Text = "

在ASP.NET里轻松实炙趼酝?lt;/h3>"; Button1.Text = "上载并显示缩略图"; } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// private void InitializeComponent() { this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void Button1_Click(object sender, System.EventArgs e) { HttpFileCollection MyFileColl = HttpContext.Current.Request.Files; HttpPostedFile MyPostedFile = MyFileColl[0]; if (MyPostedFile.ContentType.ToString().ToLower().IndexOf("image") < 0) { Response.Write("无效的图形格式。"); return; } GetThumbNail(MyPostedFile.FileName, 100, 100, MyPostedFile.ContentType.ToString(), false, MyPostedFile.InputStream); } private System.Drawing.Imaging.ImageFormat GetImageType(object strContentType) { if ((strContentType.ToString().ToLower()) == "image/pjpeg") { return System.Drawing.Imaging.ImageFormat.Jpeg; } else if ((strContentType.ToString().ToLower()) == "image/gif") { return System.Drawing.Imaging.ImageFormat.Gif; } else if ((strContentType.ToString().ToLower()) == "image/bmp") { return System.Drawing.Imaging.ImageFormat.Bmp; } else if ((strContentType.ToString().ToLower()) == "image/tiff") { return System.Drawing.Imaging.ImageFormat.Tiff; } else if ((strContentType.ToString().ToLower()) == "image/x-icon") { return System.Drawing.Imaging.ImageFormat.Icon; } else if ((strContentType.ToString().ToLower()) == "image/x-png") { return System.Drawing.Imaging.ImageFormat.Png; } else if ((strContentType.ToString().ToLower()) == "image/x-emf") { return System.Drawing.Imaging.ImageFormat.Emf; } else if ((strContentType.ToString().ToLower()) == "image/x-exif") { return System.Drawing.Imaging.ImageFormat.Exif; } else if ((strContentType.ToString().ToLower()) == "image/x-wmf") { return System.Drawing.Imaging.ImageFormat.Wmf; } else { return System.Drawing.Imaging.ImageFormat.MemoryBmp; } } private void GetThumbNail(string strFileName, int iWidth, int iheight, string strContentType, bool blnGetFromFile, System.IO.Stream ImgStream) { System.Drawing.Image oImg; if (blnGetFromFile) { oImg = System.Drawing.Image.FromFile(strFileName); } else { oImg = System.Drawing.Image.FromStream(ImgStream); } oImg = oImg.GetThumbnailImage(iWidth, iheight, null, IntPtr.Zero); string strGuid = System.Guid.NewGuid().ToString().ToUpper(); string strFileExt = strFileName.Substring(strFileName.LastIndexOf(".")); Response.ContentType = strContentType; MemoryStream MemStream = new MemoryStream(); oImg.Save(MemStream, GetImageType(strContentType)); MemStream.WriteTo(Response.OutputStream); } } } 功能: 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[i]; 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[i].Selected == false)) { erroNumber = erroNumber + 1; picInfo.Append("错误:文件("+ (i+1) +") " + imgNameOnly + " 已经存在,请修改文件名
" ); } } else { erroNumber = erroNumber + 1; picInfo.Append("错误:文件("+ (i+1) +") " + imgNameOnly + " 扩展名 " + fileExt + " 不被许可
" ); } } if(erroNumber > 0) { picInfo.Append("全部操作均未完成,请修改错误,再进行操作
"); } else { for(int i = 0;i < Request.Files.Count; i++) { HttpPostedFile PostedFile = Request.Files[i]; 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("文件 名:" + imgNameOnly + " ( " + oriImg.Width + " x " + oriImg.Height + " ) " + PostedFile.ContentLength/1024 + "KB
"); picInfo.Append("缩略图名:" + imgThumbnail + " ( " + newImg.Width + " x " + newImg.Height + " )

"); oriImg.Dispose(); newImg.Dispose(); }//for picInfo.Append("所有操作成功
"); }// if erronumber = 0 } else { picInfo.Append("有错误,请检查。操作未成功
"); } lblPicInfo.Text = picInfo.ToString(); }
资料引用:http://www.knowsky.com/5723.html

string json = ""; string newfilename = ""; string path = ""; try { if (context.Request.Files["file_upload"] != null && context.Request.Files["file_upload"].FileName != "") { string hzm = System.IO.Path.GetExtension(context.Request.Files["file_upload"].FileName);//后缀名 如 .doc string[] a = { ".txt", ".jpg", ".jpeg", ".gif", ".png", ".docx", ".doc", ".xlsx", ".xls", ".rar", ".zip",".pdf" };//设定好了的格式 if (!a.Contains(hzm)) { json = "{\"statusCode\":\"300\",\"message\":\"文件格式不正确\",\"navTabId\":\"nav6\",\"rel\":\"\",\"callbackType\":\"\",\"forwardUrl\":\"\"}"; return json; } else { int defaulsize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["filesize"]);//取得设置的默认文件的大小 int filesize = (context.Request.Files["file_upload"].ContentLength) / 1024; //取得上传的文件的大小,单位为bytes if (filesize < defaulsize) { #region 对文件进行操作 newfilename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + hzm;//文件的新名字 如20120711105734222.doc path = System.Web.HttpContext.Current.Server.MapPath("~/UploadFile//");//文件保存的路径 if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } #endregion } else { //超过了文件的大小 json = "{\"statusCode\":\"300\",\"message\":\"上传的文件超过了3000M,请重新选择\",\"navTabId\":\"nav6\",\"rel\":\"\",\"callbackType\":\"\",\"forwardUrl\":\"\"}"; return json; } } } } catch (Exception) { json = "{\"statusCode\":\"300\",\"message\":\"文件格式不正确\",\"navTabId\":\"nav6\",\"rel\":\"\",\"callbackType\":\"\",\"forwardUrl\":\"\"}"; return json; } if (newfilename != "") { context.Request.Files["file_upload"].SaveAs(path + newfilename); //保存文件 }

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧