介绍ASP.NET上传
创始人
2024-04-22 04:51:50
0

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. ///  
  94. /// 提示关闭层  
  95. /// 
  96.  
  97. ///  name="MsgStr"> 
  98. private void Alert(string MsgStr)  
  99. {  
  100.  
  101. Response.Write("");  
  102. Response.Write("");  
  103. Response.Write("</FONT></STRONG>error<STRONG><FONT color=#006699>");  
  104. Response.Write(" http-equiv=\"content-type\" content=\"text/html;
     charset=utf-8\">");  
  105. Response.Write("");  
  106. Response.Write("");  
  107. Response.Write(" type=\"text/javascript\">alert(\"" + MsgStr + "\");
    parent.KindDisableMenu();parent.KindReloadIframe();");  
  108. Response.Write("");  
  109. Response.Write("");  
  110. }  
  111. ///  
  112. /// 检测文件类型  
  113. /// 
  114.  
  115. ///  name="ExtStr"> 
  116. ///  name="fileExt"> 
  117. ///  
  118. private bool CheckExt(string[] ExtStr,string fileExt)  
  119. {  
  120. for (int i = 0; i < ExtStr.Length; i++)  
  121. {  
  122. if (ExtStr[i] != fileExt)  
  123. {  
  124. return true;  
  125. }  
  126. }  
  127. return false;  
  128. }  
  129. ///  
  130. /// 返回图片  
  131. /// 
  132.  
  133. ///  name="FileUrl"> 
  134. ///  name="FileWidth"> 
  135. ///  name="FileHeight"> 
  136. ///  name="FileBorder"> 
  137. ///  name="FileTitle"> 
  138. ///  name="FileAlign"> 
  139. ///  name="FileHspace"> 
  140. ///  name="FileVspace"> 
  141. private void ReturnImg( string FileUrl,string FileWidth,string FileHeight,
    string FileBorder,string FileTitle,string FileAlign,string FileHspace,string FileVspace)  
  142. {  
  143. Response.Write("");  
  144. Response.Write("");  
  145. Response.Write("</FONT></STRONG>上传成功<STRONG><FONT color=#006699>");  
  146. Response.Write(" http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">");  
  147. Response.Write("");  
  148. Response.Write("");  
  149. Response.Write(" type=\"text/javascript\">parent.KindInsertImage
    (\"" + FileUrl +"\",\"" + FileWidth + "\",\"" + FileHeight + "\",\"" + 
    FileBorder + "\",\"" + FileTitle + "\",\"" + FileAlign + "\",\"" + 
    FileHspace + "\",\"" + FileVspace + "\");");  
  150. Response.Write("");  
  151. Response.Write("");  
  152. }  

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

【编辑推荐】

  1. ASP.NET中的数据源控件
  2. 介绍ASP.NET的XML Web服务使用
  3. ASP.NET应用程序的web.config文件
  4. 概述ASP.NET XML Web服务
  5. ASP.NET中实现HTTP请求

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...