123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
-
- using System;
- using System.Collections.Generic;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.IO;
- using System.Net;
- using iNethinkCMS.Web.UI;
- namespace iNethinkCMS.Web.inc
- {
- public partial class upload_remote : Admin_BasePage
- {
- iNethinkCMS.BLL.BLL_iNethinkCMS_Upload bll = new iNethinkCMS.BLL.BLL_iNethinkCMS_Upload();
- iNethinkCMS.Model.Model_iNethinkCMS_Upload model = new iNethinkCMS.Model.Model_iNethinkCMS_Upload();
- private string upExt;
- private string attachDir;
- private int dirType;
- private int maxAttachSize;
- protected void Page_Load(object sender, EventArgs e)
- {
- CheckUserPower("a");
- upExt = ",jpg,jpeg,gif,png,bmp,";
- attachDir = "/upload";
- dirType = 1;
- maxAttachSize = Convert.ToInt32(siteConfig.UpFileMaxSize) * 1024 * 1024;
- Response.Charset = "UTF-8";
- string[] arrUrl = Request["urls"].Split('|');
- for (int i = 0; i < arrUrl.Length; i++)
- {
- string localUrl = saveRemoteImg(arrUrl[i]);
- if (localUrl != "")
- {
- arrUrl[i] = localUrl;
- }
- }
- Response.Write(String.Join("|", arrUrl));
- Response.End();
- }
- string saveRemoteImg(string sUrl)
- {
- byte[] fileContent;
- string sExt;
- string sFile;
- if (sUrl.StartsWith("data:image"))
- {
-
- int pstart = sUrl.IndexOf('/') + 1;
- sExt = sUrl.Substring(pstart, sUrl.IndexOf(';') - pstart).ToLower();
- if (upExt.IndexOf("," + sExt + ",") == -1)
- {
- return "";
- }
- fileContent = Convert.FromBase64String(sUrl.Substring(sUrl.IndexOf("base64,") + 7));
- }
- else
- {
-
- sExt = sUrl.Substring(sUrl.LastIndexOf('.') + 1).ToLower();
- if (upExt.IndexOf("," + sExt + ",") == -1)
- {
- return "";
- }
- fileContent = getUrl(sUrl);
- }
- if (fileContent == null)
- {
- return "";
- }
-
- if (fileContent.Length > maxAttachSize)
- {
- return "";
- }
-
- sFile = getLocalPath(sExt);
- File.WriteAllBytes(Server.MapPath(sFile), fileContent);
-
- model.UpType = 0;
- model.Aid = 0;
- model.Cid = 0;
- model.Dir = sFile;
- model.Ext = sExt;
- model.Time = DateTime.Now;
- bll.Add(model);
- return sFile;
- }
- string getLocalPath(string extension)
- {
- string attach_dir, attach_subdir, filename;
- switch (dirType)
- {
- case 1:
- attach_subdir = "day_" + DateTime.Now.ToString("yyyyMMdd");
- break;
- case 2:
- attach_subdir = "month_" + DateTime.Now.ToString("yyMM");
- break;
- default:
- attach_subdir = "ext_" + extension;
- break;
- }
- attach_dir = attachDir + "/" + attach_subdir + "/";
- if (!Directory.Exists(Server.MapPath(attach_dir)))
- {
- Directory.CreateDirectory(Server.MapPath(attach_dir));
- }
-
- Random random = new Random(DateTime.Now.Millisecond);
- filename = "r" + DateTime.Now.ToString("yyyyMMddhhmmss") + random.Next(10000) + "." + extension;
- return attach_dir + filename;
- }
- byte[] getUrl(string sUrl)
- {
- WebClient wc = new WebClient();
- try
- {
- return wc.DownloadData(sUrl);
- }
- catch
- {
- return null;
- }
- }
- }
- }
|