ASP.NET上传实例介绍

发布时间:2021-08-21 09:29:51 作者:chen
阅读:105
开发者专用服务器限时活动,0元免费领! 查看>>

本篇内容介绍了“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. public partial class Jscript_KindEditor_upload_cgi_upload : System.Web.UI.Page  

  13. {  

  14. protected void Page_Load(object sender, EventArgs e)  

  15. {  

  16. //文件保存目录路径  

  17. string SavePath = "/Upload_Images/";  

  18. //文件保存目录URL  

  19. string SaveUrl = "/Upload_Images/";  

  20. //上传图片类型  

  21. string[] ExtStr=new string[4];  

  22. ExtStr[0] = ".gif";  

  23. ExtStr[1] = ".jpg";  

  24. ExtStr[2] = ".png";  

  25. ExtStr[3] = ".bmp";  

  26. //图片的***大小  

  27. int MaxSize = 100000;  

  28. //错误提示  

  29. string[] MsgStr = new string[3];  

  30. MsgStr[0] = "上传文件大小超过限制.";  

  31. MsgStr[1] = "上传文件扩展名是不允许的扩展名.";  

  32. MsgStr[2] = "上传文件失败\\n请重试.";  

  33. string imgWidth = Request.Form["imgWidth"];  

  34. string imgHeight = Request.Form["imgHeight"];  

  35. string imgBorder = Request.Form["imgBorder"];  

  36. string imgTitle = Request.Form["imgTitle"];  

  37. string imgAlign = Request.Form["imgAlign"];  

  38. string imgHspace = Request.Form["imgHspace"];  

  39. string imgVspace = Request.Form["imgVspace"];  

  40. HttpPostedFile imgFile = HttpContext.Current.Request.Files["imgFile"];  

  41. //获得文件名  

  42. string FileName = System.IO.Path.GetFileName(imgFile.FileName);  

  43. if (FileName != "")  

  44. {  

  45. if (imgFile.ContentLength > MaxSize)  

  46. {  

  47. Alert(MsgStr[0]);  

  48. }  

  49. else  

  50. {  

  51. string fileExtension = System.IO.Path.GetExtension(FileName).ToLower();  

  52. if (CheckExt(ExtStr, fileExtension))  

  53. {  

  54. //重新为文件命名,时间毫秒部分+扩展名  

  55. string imgReName = "" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff", 
    DateTimeFormatInfo.InvariantInfo) + "" + fileExtension;  

  56. //文件夹名  

  57. string imgFolderName=DateTime.Now.ToString
    ("yyyyMMdd",DateTimeFormatInfo.InvariantInfo);  

  58. try  

  59. {  

  60. if (!System.IO.Directory.Exists(@Server.MapPath
    ("" + SavePath + "" +imgFolderName + "")))  

  61. {  

  62. //生成文件完整目录  

  63. System.IO.Directory.CreateDirectory(@Server.MapPath
    ("" + SavePath + "" +imgFolderName + ""));  

  64. }  

  65. imgFile.SaveAs(@Server.MapPath
    ("" + SavePath + "" + imgFolderName + "/")+imgReName);  

  66. }  

  67. catch  

  68. {  

  69. Alert(MsgStr[2]);  

  70. }  

  71. string imgUrl = SaveUrl + imgFolderName + "/" + imgReName;  

  72. ReturnImg(imgUrl, imgWidth, imgHeight, imgBorder, 
    imgTitle, imgAlign, imgHspace, imgVspace);  

  73. }  

  74. else  

  75. {  

  76. Alert(MsgStr[1]);  

  77. }  

  78. }  

  79. }  

  80. }  

  81. /// <summary> 

  82. /// 提示关闭层  

  83. /// </summary> 

  84. /// <param name="MsgStr"></param> 

  85. private void Alert(string MsgStr)  

  86. {  

  87. Response.Write("<html>");  

  88. Response.Write("<head>");  

  89. Response.Write("<title>error</title>");  

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

  91. Response.Write("</head>");  

  92. Response.Write("<body>");  

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

  94. Response.Write("</body>");  

  95. Response.Write("</html>");  

  96. }  

  97. /// <summary> 

  98. /// 检测文件类型  

  99. /// </summary> 

  100. /// <param name="ExtStr"></param> 

  101. /// <param name="fileExt"></param> 

  102. /// <returns></returns> 

  103. private bool CheckExt(string[] ExtStr,string fileExt)  

  104. {  

  105. for (int i = 0; i < ExtStr.Length; i++)  

  106. {  

  107. if (ExtStr[i] != fileExt)  

  108. {  

  109. return true;  

  110. }  

  111. }  

  112. return false;  

  113. }  

  114. /// <summary> 

  115. /// 返回图片  

  116. /// </summary> 

  117. /// <param name="FileUrl"></param> 

  118. /// <param name="FileWidth"></param> 

  119. /// <param name="FileHeight"></param> 

  120. /// <param name="FileBorder"></param> 

  121. /// <param name="FileTitle"></param> 

  122. /// <param name="FileAlign"></param> 

  123. /// <param name="FileHspace"></param> 

  124. /// <param name="FileVspace"></param> 

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

  126. {  

  127. Response.Write("<html>");  

  128. Response.Write("<head>");  

  129. Response.Write("<title>上传成功</title>");  

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

  131. Response.Write("</head>");  

  132. Response.Write("<body>");  

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

  134. Response.Write("</body>");  

  135. Response.Write("</html>");  

  136. }  

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

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:
  1. 详细介绍如何的禁止疑似上传
  2. 实例演示图片上传

开发者交流群:

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

asp.net

上一篇:Docker镜像加速器怎么用

下一篇:redission-tomcat如何实现从单机部署到多机部署

相关阅读

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

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