温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ASP.NET上传实例介绍

发布时间:2021-08-21 09:29:51 来源:亿速云 阅读:76 作者:chen 栏目:编程语言

本篇内容介绍了“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上传实例介绍”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI