介绍ASP.NET上传

开发 后端
本文介绍ASP.NET上传,KindEditor是一个不错的网页在线编辑器,可是它只提供了asp,php,jsp上传的类,没有提供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>error</title>");  
  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上传代码,希望对大家有用。

【编辑推荐】

  1. ASP.NET中的数据源控件
  2. 介绍ASP.NET的XML Web服务使用
  3. ASP.NET应用程序的web.config文件
  4. 概述ASP.NET XML Web服务
  5. ASP.NET中实现HTTP请求
责任编辑:佚名 来源: IT168
相关推荐

2009-07-22 16:05:34

ASP.NET AJA

2009-08-19 09:23:40

ASP.NET Rou

2009-07-29 17:23:17

ASP.NET表单

2009-07-21 10:40:36

ASP.NET Pro

2009-07-23 14:17:41

2009-07-29 17:26:39

ASP.NET页面

2009-09-10 14:02:08

LINQ ASP.NE

2009-07-27 17:00:29

ASP.NET主机

2009-07-20 16:12:21

ASP.NET Fra

2009-07-29 09:14:36

ASP.NET网站

2009-08-03 17:35:07

ASP.NET WebASP.NET编程工具

2009-07-29 10:35:51

ASP.NET缓存

2009-07-20 10:53:59

ASP.NET MVC

2009-08-05 15:57:03

ASP.NET控件ID

2009-08-05 10:36:08

开发ASP.NET

2009-07-29 09:53:24

ASP.NET异常管理

2009-07-21 15:11:14

ASP.NET Rou

2009-07-24 13:39:03

ASP.NET弹出窗口

2009-07-21 15:38:31

2009-07-27 16:09:50

点赞
收藏

51CTO技术栈公众号