ChengDa_English/SGGL/FineUIPro.Web/res/umeditor/net/Uploader.cs

204 lines
5.5 KiB
C#
Raw Normal View History

2022-03-15 17:36:38 +08:00
using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Collections;
namespace FineUIPro.Examples.res.umeditor.net
{
/// <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 + DateTime.Now.ToString("yyyy-MM-dd") + "/";
uploadpath = cxt.Server.MapPath(pathbase);//获取文件上传路径
try
{
uploadFile = cxt.Request.Files[0];
originalName = uploadFile.FileName;
//目录创建
createFolder();
//格式验证
if (checkType(filetype))
{
state = "不允许的文件类型";
}
//大小验证
if (checkSize(size))
{
state = "文件大小超出网站限制";
}
//保存图片
if (state == "SUCCESS")
{
filename = reName();
uploadFile.SaveAs(uploadpath + filename);
URL = pathbase + filename;
}
}
catch (Exception)
{
state = "未知错误";
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)
{
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);
infoList.Add("originalName", originalName);
infoList.Add("name", Path.GetFileName(URL));
infoList.Add("size", uploadFile.ContentLength);
infoList.Add("type", Path.GetExtension(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);
//}
}
}
}