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. /// 

     

  94. /// 提示关闭层  

  95. ///  

  96. ///  name="MsgStr"> 

  97. private void Alert(string MsgStr)  

  98. {  

  99.  

  100. Response.Write("");  

  101. Response.Write("");  

  102. Response.Write("</strong>error<strong>");  

  103. Response.Write(" http-equiv=\"content-type\" content=\"text/html;
    charset=utf-8\">");  

  104. Response.Write("");  

  105. Response.Write("");  

  106. Response.Write(" type=\"text/javascript\">alert(\"" + MsgStr + "\");
    parent.KindDisableMenu();parent.KindReloadIframe();");  

  107. Response.Write("");  

  108. Response.Write("");  

  109. }  

  110. /// 

     

  111. /// 检测文件类型  

  112. ///  

  113. ///  name="ExtStr"> 

  114. ///  name="fileExt"> 

  115. ///  

  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. /// 

     

  128. /// 返回图片  

  129. ///  

  130. ///  name="FileUrl"> 

  131. ///  name="FileWidth"> 

  132. ///  name="FileHeight"> 

  133. ///  name="FileBorder"> 

  134. ///  name="FileTitle"> 

  135. ///  name="FileAlign"> 

  136. ///  name="FileHspace"> 

  137. ///  name="FileVspace"> 

  138. private void ReturnImg( string FileUrl,string FileWidth,string FileHeight,
    string FileBorder,string FileTitle,string FileAlign,string FileHspace,string FileVspace)  

  139. {  

  140. Response.Write("");  

  141. Response.Write("");  

  142. Response.Write("</strong>上传成功<strong>");  

  143. Response.Write(" http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">");  

  144. Response.Write("");  

  145. Response.Write("");  

  146. Response.Write(" type=\"text/javascript\">parent.KindInsertImage
    (\"" + FileUrl +"\",\"" + FileWidth + "\",\"" + FileHeight + "\",\"" + 
    FileBorder + "\",\"" + FileTitle + "\",\"" + FileAlign + "\",\"" + 
    FileHspace + "\",\"" + FileVspace + "\");");  

  147. Response.Write("");  

  148. Response.Write("");  

  149. }  

“ASP.NET上传实例介绍”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


新闻标题:ASP.NET上传实例介绍
本文链接:http://bzwzjz.com/article/pcpdcg.html