介绍ASP.NET上传


  本文标签:ASP.NET上传

  KindEditor是一个不错的网页在线编辑器,可是它只提供了asp,php,jsp上传的类,没有提供ASP.NET上传的类  。

  ASP.NET上传代码:

  1. using System;  
  2. using System.Globalization;  
  3. using System.Collections;  
  4. using System.Configuration;  
  5. using System.Data;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12.  
  13. public partial class Jscript_KindEditor_upload_cgi_upload : System.Web.UI.Page  
  14. {  
  15. protected void Page_Load(object sender, EventArgs e)  
  16. {  
  17. //文件保存目录路径  
  18. string SavePath = "/Upload_Images/";  
  19. //文件保存目录URL  
  20. string SaveUrl = "/Upload_Images/";  
  21. //上传图片类型  
  22. string[] ExtStr=new string[4];  
  23. ExtStr[0] = ".gif";  
  24. ExtStr[1] = ".jpg";  
  25. ExtStr[2] = ".png";  
  26. ExtStr[3] = ".bmp";  
  27. //图片的最大大小  
  28. int MaxSize = 100000;  
  29. //错误提示  
  30. string[] MsgStr = new string[3];  
  31. MsgStr[0] = "上传文件大小超过限制.";  
  32. MsgStr[1] = "上传文件扩展名是不允许的扩展名.";  
  33. MsgStr[2] = "上传文件失败\\n请重试.";  
  34.  
  35. string imgWidth = Request.Form["imgWidth"];  
  36. string imgHeight = Request.Form["imgHeight"];  
  37. string imgBorder = Request.Form["imgBorder"];  
  38. string imgTitle = Request.Form["imgTitle"];  
  39. string imgAlign = Request.Form["imgAlign"];  
  40. string imgHspace = Request.Form["imgHspace"];  
  41. string imgVspace = Request.Form["imgVspace"];  
  42.  
  43. HttpPostedFile imgFile = HttpContext.Current.Request.Files["imgFile"];  
  44. //获得文件名  
  45. string FileName = System.IO.Path.GetFileName(imgFile.FileName);  
  46.  
  47. if (FileName != "")  
  48. {  
  49. if (imgFile.ContentLength > MaxSize)  
  50. {  
  51. Alert(MsgStr[0]);  
  52. }  
  53. else  
  54. {  
  55. string fileExtension = System.IO.Path.GetExtension(FileName).ToLower();  
  56. if (CheckExt(ExtStr, fileExtension))  
  57. {  
  58. //重新为文件命名,时间毫秒部分+扩展名  
  59. string imgReName = "" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff", 
    DateTimeFormatInfo.InvariantInfo) + "" + fileExtension;  
  60. //文件夹名  
  61. string imgFolderName=DateTime.Now.ToString
    ("yyyyMMdd",DateTimeFormatInfo.InvariantInfo);  
  62.  
  63. try  
  64. {  
  65.  
  66. if (!System.IO.Directory.Exists(@Server.MapPath
    ("" + SavePath + "" +imgFolderName + "")))  
  67. {  
  68. //生成文件完整目录  
  69. System.IO.Directory.CreateDirectory(@Server.MapPath
    ("" + SavePath + "" +imgFolderName + ""));  
  70. }  
  71.  
  72. imgFile.SaveAs(@Server.MapPath
    ("" + SavePath + "" + imgFolderName + "/")+imgReName);  
  73.  
  74.  
  75. }  
  76. catch  
  77. {  
  78. Alert(MsgStr[2]);  
  79. }  
  80. string imgUrl = SaveUrl + imgFolderName + "/" + imgReName;  
  81. ReturnImg(imgUrl, imgWidth, imgHeight, imgBorder, 
    imgTitle, imgAlign, imgHspace, imgVspace);  
  82.  
  83. }  
  84. else  
  85. {  
  86. Alert(MsgStr[1]);  
  87. }  
  88. }  
  89. }  
  90.  
  91.  
  92. }  
  93. /// <summary> 
  94. /// 提示关闭层  
  95. /// summary> 
  96. /// <param name="MsgStr">param> 
  97. private void Alert(string MsgStr)  
  98. {  
  99.  
  100. Response.Write("<html>");  
  101. Response.Write("<head>");  
  102. Response.Write("<title>errortitle>");  
  103. Response.Write("<meta http-equiv=\"content-type\" content=\"text/html;
    charset=utf-8\">");  
  104. Response.Write("head>");  
  105. Response.Write("<body>");  
  106. Response.Write("<script type=\"text/javascript\">alert(\"" + MsgStr + "\");
    parent.KindDisableMenu();parent.KindReloadIframe();
    script>");  
  107. Response.Write("body>");  
  108. Response.Write("html>");  
  109. }  
  110. /// <summary> 
  111. /// 检测文件类型  
  112. /// summary> 
  113. /// <param name="ExtStr">param> 
  114. /// <param name="fileExt">param> 
  115. /// <returns>returns> 
  116. private bool CheckExt(string[] ExtStr,string fileExt)  
  117. {  
  118. for (int i = 0; i < ExtStr.Length; i++)  
  119. {  
  120. if (ExtStr[i] != fileExt)  
  121. {  
  122. return true;  
  123. }  
  124. }  
  125. return false;  
  126. }  
  127. /// <summary> 
  128. /// 返回图片  
  129. /// summary> 
  130. /// <param name="FileUrl">param> 
  131. /// <param name="FileWidth">param> 
  132. /// <param name="FileHeight">param> 
  133. /// <param name="FileBorder">param> 
  134. /// <param name="FileTitle">param> 
  135. /// <param name="FileAlign">param> 
  136. /// <param name="FileHspace">param> 
  137. /// <param name="FileVspace">param> 
  138. private void ReturnImg( string FileUrl,string FileWidth,string FileHeight,
    string FileBorder,string FileTitle,string FileAlign,string FileHspace,string FileVspace)  
  139. {  
  140. Response.Write("<html>");  
  141. Response.Write("<head>");  
  142. Response.Write("<title>上传成功title>");  
  143. Response.Write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">");  
  144. Response.Write("head>");  
  145. Response.Write("<body>");  
  146. Response.Write("<script type=\"text/javascript\">parent.KindInsertImage
    (\"" + FileUrl +"\",\"" + FileWidth + "\",\"" + FileHeight + "\",\"" + 
    FileBorder + "\",\"" + FileTitle + "\",\"" + FileAlign + "\",\"" + 
    FileHspace + "\",\"" + FileVspace + "\");
    script>");  
  147. Response.Write("body>");  
  148. Response.Write("html>");  
  149. }  

  ASP.NET上传代码,希望对大家有用  。