upload_remote.aspx.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*******************************************************************************
  2. * iNethinkCMS - 网站内容管理系统
  3. * Copyright (C) 2012-2013 inethink.com
  4. *
  5. * @author jackyang <69991000@qq.com>
  6. * @website http://cms.inethink.com
  7. * @version 1.3.6.0 (2013-08-14)
  8. *
  9. * This is licensed under the GNU LGPL, version 3.0 or later.
  10. * For details, see: http://www.gnu.org/licenses/gpl-3.0.html
  11. *******************************************************************************/
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Web;
  15. using System.Web.UI;
  16. using System.Web.UI.WebControls;
  17. using System.IO;
  18. using System.Net;
  19. using iNethinkCMS.Web.UI;
  20. namespace iNethinkCMS.Web.inc
  21. {
  22. public partial class upload_remote : Admin_BasePage
  23. {
  24. iNethinkCMS.BLL.BLL_iNethinkCMS_Upload bll = new iNethinkCMS.BLL.BLL_iNethinkCMS_Upload();
  25. iNethinkCMS.Model.Model_iNethinkCMS_Upload model = new iNethinkCMS.Model.Model_iNethinkCMS_Upload();
  26. private string upExt; //上传扩展名
  27. private string attachDir; //上传文件保存路径,结尾不要带/
  28. private int dirType; // 1:按天存入目录 2:按月存入目录 3:按扩展名存目录 建议使用按天存
  29. private int maxAttachSize; // 最大上传大小,默认是2M
  30. protected void Page_Load(object sender, EventArgs e)
  31. {
  32. CheckUserPower("a");
  33. upExt = ",jpg,jpeg,gif,png,bmp,";
  34. attachDir = "/upload";
  35. dirType = 1;
  36. maxAttachSize = Convert.ToInt32(siteConfig.UpFileMaxSize) * 1024 * 1024;
  37. Response.Charset = "UTF-8";
  38. string[] arrUrl = Request["urls"].Split('|');
  39. for (int i = 0; i < arrUrl.Length; i++)
  40. {
  41. string localUrl = saveRemoteImg(arrUrl[i]);
  42. if (localUrl != "")
  43. {
  44. arrUrl[i] = localUrl;//有效图片替换
  45. }
  46. }
  47. Response.Write(String.Join("|", arrUrl));
  48. Response.End();
  49. }
  50. string saveRemoteImg(string sUrl)
  51. {
  52. byte[] fileContent;
  53. string sExt;
  54. string sFile;
  55. if (sUrl.StartsWith("data:image"))
  56. {
  57. // base64编码的图片,可能出现在firefox粘贴,或者某些网站上,例如google图片
  58. int pstart = sUrl.IndexOf('/') + 1;
  59. sExt = sUrl.Substring(pstart, sUrl.IndexOf(';') - pstart).ToLower();
  60. if (upExt.IndexOf("," + sExt + ",") == -1)
  61. {
  62. return "";
  63. }
  64. fileContent = Convert.FromBase64String(sUrl.Substring(sUrl.IndexOf("base64,") + 7));
  65. }
  66. else
  67. {
  68. // 图片网址
  69. sExt = sUrl.Substring(sUrl.LastIndexOf('.') + 1).ToLower();
  70. if (upExt.IndexOf("," + sExt + ",") == -1)
  71. {
  72. return "";
  73. }
  74. fileContent = getUrl(sUrl);
  75. }
  76. if (fileContent == null)
  77. {
  78. return "";
  79. }
  80. //超过最大上传大小忽略
  81. if (fileContent.Length > maxAttachSize)
  82. {
  83. return "";
  84. }
  85. //有效图片保存
  86. sFile = getLocalPath(sExt);
  87. File.WriteAllBytes(Server.MapPath(sFile), fileContent);
  88. //存入数据库
  89. model.UpType = 0;
  90. model.Aid = 0;
  91. model.Cid = 0;
  92. model.Dir = sFile;
  93. model.Ext = sExt;
  94. model.Time = DateTime.Now;
  95. bll.Add(model);
  96. return sFile;
  97. }
  98. string getLocalPath(string extension)
  99. {
  100. string attach_dir, attach_subdir, filename;
  101. switch (dirType)
  102. {
  103. case 1:
  104. attach_subdir = "day_" + DateTime.Now.ToString("yyyyMMdd");
  105. break;
  106. case 2:
  107. attach_subdir = "month_" + DateTime.Now.ToString("yyMM");
  108. break;
  109. default:
  110. attach_subdir = "ext_" + extension;
  111. break;
  112. }
  113. attach_dir = attachDir + "/" + attach_subdir + "/";
  114. if (!Directory.Exists(Server.MapPath(attach_dir)))
  115. {
  116. Directory.CreateDirectory(Server.MapPath(attach_dir));
  117. }
  118. // 生成随机文件名
  119. Random random = new Random(DateTime.Now.Millisecond);
  120. filename = "r" + DateTime.Now.ToString("yyyyMMddhhmmss") + random.Next(10000) + "." + extension;
  121. return attach_dir + filename;
  122. }
  123. byte[] getUrl(string sUrl)
  124. {
  125. WebClient wc = new WebClient();
  126. try
  127. {
  128. return wc.DownloadData(sUrl);
  129. }
  130. catch
  131. {
  132. return null;
  133. }
  134. }
  135. }
  136. }