|
Server : Microsoft-IIS/10.0 System : Windows NT EBS-6136 10.0 build 17763 (Windows Server 2016) i586 User : jxgj ( 0) PHP Version : 7.0.33 Disable Function : passthru,exec,system,shell_exec,proc_open,popen,pcntl_exec,socket_bind,stream_socket_server Directory : D:/tmp_aspnet/root/a94d487b/c2585c4e/ |
?#pragma checksum "D:\wwwroot\mhkguanlin\wwwroot\App_Code\Uploader.cs" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "4BD974CA1D791897813FED7ACA9E8BB7B9730E08"
#line 1 "D:\wwwroot\mhkguanlin\wwwroot\App_Code\Uploader.cs"
using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
using System.Linq;
/// <summary>
/// UEditor缂栬緫鍣ㄩ氱敤涓婁紶绫?
/// </summary>
public class Uploader
{
string state = "SUCCESS";
string URL = null;
string currentType = null;
string uploadpath = null;
string filename = null;
string originalName = null;
HttpPostedFile uploadFile = null;
/**
* 涓婁紶鏂囦欢鐨勪富澶勭悊鏂规硶
* @param HttpContext
* @param string
* @param string[]
*@param int
* @return Hashtable
*/
public Hashtable upFile(HttpContext cxt, string pathbase, string[] filetype, int size)
{
pathbase = pathbase + "/";
uploadpath = cxt.Server.MapPath(pathbase);//鑾峰彇鏂囦欢涓婁紶璺緞
try
{
uploadFile = cxt.Request.Files[0];
originalName = uploadFile.FileName;
//鐩綍鍒涘缓
createFolder();
//鏍煎紡楠岃瘉
if (checkType(filetype))
{
//涓嶅厑璁哥殑鏂囦欢绫诲瀷
state = "\u4e0d\u5141\u8bb8\u7684\u6587\u4ef6\u7c7b\u578b";
}
//澶у皬楠岃瘉
if (checkSize(size))
{
//鏂囦欢澶у皬瓒呭嚭缃戠珯闄愬埗
state = "\u6587\u4ef6\u5927\u5c0f\u8d85\u51fa\u7f51\u7ad9\u9650\u5236";
}
//淇濆瓨鍥剧墖
if (state == "SUCCESS")
{
filename = NameFormater.Format(cxt.Request["fileNameFormat"], originalName);
var testname = filename;
var ai = 1;
while (File.Exists(uploadpath + testname))
{
testname = Path.GetFileNameWithoutExtension(filename) + "_" + ai++ + Path.GetExtension(filename);
}
uploadFile.SaveAs(uploadpath + testname);
URL = pathbase + testname;
}
}
catch (Exception)
{
// 鏈煡閿欒
state = "\u672a\u77e5\u9519\u8bef";
URL = "";
}
return getUploadInfo();
}
/**
* 涓婁紶娑傞甫鐨勪富澶勭悊鏂规硶
* @param HttpContext
* @param string
* @param string[]
*@param string
* @return Hashtable
*/
public Hashtable upScrawl(HttpContext cxt, string pathbase, string tmppath, string base64Data)
{
pathbase = pathbase + DateTime.Now.ToString("yyyy-MM-dd") + "/";
uploadpath = cxt.Server.MapPath(pathbase);//鑾峰彇鏂囦欢涓婁紶璺緞
FileStream fs = null;
try
{
//鍒涘缓鐩綍
createFolder();
//鐢熸垚鍥剧墖
filename = System.Guid.NewGuid() + ".png";
fs = File.Create(uploadpath + filename);
byte[] bytes = Convert.FromBase64String(base64Data);
fs.Write(bytes, 0, bytes.Length);
URL = pathbase + filename;
}
catch (Exception e)
{
state = "鏈煡閿欒";
URL = "";
}
finally
{
fs.Close();
deleteFolder(cxt.Server.MapPath(tmppath));
}
return getUploadInfo();
}
/**
* 鑾峰彇鏂囦欢淇℃伅
* @param context
* @param string
* @return string
*/
public string getOtherInfo(HttpContext cxt, string field)
{
string info = null;
if (cxt.Request.Form[field] != null && !String.IsNullOrEmpty(cxt.Request.Form[field]))
{
info = field == "fileName" ? cxt.Request.Form[field].Split(',')[1] : cxt.Request.Form[field];
}
return info;
}
/**
* 鑾峰彇涓婁紶淇℃伅
* @return Hashtable
*/
private Hashtable getUploadInfo()
{
Hashtable infoList = new Hashtable();
infoList.Add("state", state);
infoList.Add("url", URL);
if (currentType != null)
infoList.Add("currentType", currentType);
if (originalName != null)
infoList.Add("originalName", originalName);
return infoList;
}
/**
* 閲嶅懡鍚嶆枃浠?
* @return string
*/
private string reName()
{
return System.Guid.NewGuid() + getFileExt();
}
/**
* 鏂囦欢绫诲瀷妫娴?
* @return bool
*/
private bool checkType(string[] filetype)
{
currentType = getFileExt();
return Array.IndexOf(filetype, currentType) == -1;
}
/**
* 鏂囦欢澶у皬妫娴?
* @param int
* @return bool
*/
private bool checkSize(int size)
{
return uploadFile.ContentLength >= (size * 1024*1024);
}
/**
* 鑾峰彇鏂囦欢鎵╁睍鍚?
* @return string
*/
private string getFileExt()
{
string[] temp = uploadFile.FileName.Split('.');
return "." + temp[temp.Length - 1].ToLower();
}
/**
* 鎸夌収鏃ユ湡鑷姩鍒涘缓瀛樺偍鏂囦欢澶?
*/
private void createFolder()
{
if (!Directory.Exists(uploadpath))
{
Directory.CreateDirectory(uploadpath);
}
}
/**
* 鍒犻櫎瀛樺偍鏂囦欢澶?
* @param string
*/
public void deleteFolder(string path)
{
//if (Directory.Exists(path))
//{
// Directory.Delete(path, true);
//}
}
}
public static class NameFormater
{
public static string Format(string format, string filename)
{
if (String.IsNullOrWhiteSpace(format))
{
format = "{filename}{rand:6}";
}
string ext = Path.GetExtension(filename);
filename = Path.GetFileNameWithoutExtension(filename);
format = format.Replace("{filename}", filename);
format = new Regex(@"\{rand(\:?)(\d+)\}", RegexOptions.Compiled).Replace(format, new MatchEvaluator(delegate(Match match)
{
var digit = 6;
if (match.Groups.Count > 2)
{
digit = Convert.ToInt32(match.Groups[2].Value);
}
var rand = new Random();
return rand.Next((int)Math.Pow(10, digit), (int)Math.Pow(10, digit + 1)).ToString();
}));
format = format.Replace("{time}", DateTime.Now.Ticks.ToString());
format = format.Replace("{yyyy}", DateTime.Now.Year.ToString());
format = format.Replace("{yy}", (DateTime.Now.Year % 100).ToString("D2"));
format = format.Replace("{mm}", DateTime.Now.Month.ToString("D2"));
format = format.Replace("{dd}", DateTime.Now.Day.ToString("D2"));
format = format.Replace("{hh}", DateTime.Now.Hour.ToString("D2"));
format = format.Replace("{ii}", DateTime.Now.Minute.ToString("D2"));
format = format.Replace("{ss}", DateTime.Now.Second.ToString("D2"));
var invalidPattern = new Regex(@"[\\\/\:\*\?\042\<\>\|]");
format = invalidPattern.Replace(format, "");
return format + ext;
}
}
#line default
#line hidden