202 lines
4.6 KiB
C#
202 lines
4.6 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Web;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Collections;
|
|||
|
|
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// UEditor<6F>༭<EFBFBD><E0BCAD>ͨ<EFBFBD><CDA8><EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD>
|
|||
|
|
/// </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;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* <EFBFBD>ϴ<EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
* @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);//<2F><>ȡ<EFBFBD>ļ<EFBFBD><C4BC>ϴ<EFBFBD>·<EFBFBD><C2B7>
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
uploadFile = cxt.Request.Files[0];
|
|||
|
|
originalName = uploadFile.FileName;
|
|||
|
|
|
|||
|
|
//Ŀ¼<C4BF><C2BC><EFBFBD><EFBFBD>
|
|||
|
|
createFolder();
|
|||
|
|
|
|||
|
|
//<2F><>ʽ<EFBFBD><CABD>֤
|
|||
|
|
if (checkType(filetype))
|
|||
|
|
{
|
|||
|
|
state = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>";
|
|||
|
|
}
|
|||
|
|
//<2F><>С<EFBFBD><D0A1>֤
|
|||
|
|
if (checkSize(size))
|
|||
|
|
{
|
|||
|
|
state = "<22>ļ<EFBFBD><C4BC><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>վ<EFBFBD><D5BE><EFBFBD><EFBFBD>";
|
|||
|
|
}
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>ͼƬ
|
|||
|
|
if (state == "SUCCESS")
|
|||
|
|
{
|
|||
|
|
filename = reName();
|
|||
|
|
uploadFile.SaveAs(uploadpath + filename);
|
|||
|
|
URL = pathbase + filename;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception e)
|
|||
|
|
{
|
|||
|
|
state = "δ֪<CEB4><D6AA><EFBFBD><EFBFBD>"+e.Message;
|
|||
|
|
URL = "";
|
|||
|
|
}
|
|||
|
|
return getUploadInfo();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* <EFBFBD>ϴ<EFBFBD>Ϳѻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
* @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);//<2F><>ȡ<EFBFBD>ļ<EFBFBD><C4BC>ϴ<EFBFBD>·<EFBFBD><C2B7>
|
|||
|
|
FileStream fs = null;
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>Ŀ¼
|
|||
|
|
createFolder();
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>ͼƬ
|
|||
|
|
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 = "δ֪<CEB4><D6AA><EFBFBD><EFBFBD>"+e.Message;
|
|||
|
|
URL = "";
|
|||
|
|
}
|
|||
|
|
finally
|
|||
|
|
{
|
|||
|
|
fs.Close();
|
|||
|
|
deleteFolder(cxt.Server.MapPath(tmppath));
|
|||
|
|
}
|
|||
|
|
return getUploadInfo();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* <EFBFBD><EFBFBD>ȡ<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>Ϣ
|
|||
|
|
* @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;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* <EFBFBD><EFBFBD>ȡ<EFBFBD>ϴ<EFBFBD><EFBFBD><EFBFBD>Ϣ
|
|||
|
|
* @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;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
|
|||
|
|
* @return string
|
|||
|
|
*/
|
|||
|
|
private string reName()
|
|||
|
|
{
|
|||
|
|
return System.Guid.NewGuid() + getFileExt();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* <EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
* @return bool
|
|||
|
|
*/
|
|||
|
|
private bool checkType(string[] filetype)
|
|||
|
|
{
|
|||
|
|
currentType = getFileExt();
|
|||
|
|
return Array.IndexOf(filetype, currentType) == -1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* <EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
* @param int
|
|||
|
|
* @return bool
|
|||
|
|
*/
|
|||
|
|
private bool checkSize(int size)
|
|||
|
|
{
|
|||
|
|
return uploadFile.ContentLength >= (size * 1024*1024);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* <EFBFBD><EFBFBD>ȡ<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>չ<EFBFBD><EFBFBD>
|
|||
|
|
* @return string
|
|||
|
|
*/
|
|||
|
|
private string getFileExt()
|
|||
|
|
{
|
|||
|
|
string[] temp = uploadFile.FileName.Split('.');
|
|||
|
|
return "." + temp[temp.Length - 1].ToLower();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>洢<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
*/
|
|||
|
|
private void createFolder()
|
|||
|
|
{
|
|||
|
|
if (!Directory.Exists(uploadpath))
|
|||
|
|
{
|
|||
|
|
Directory.CreateDirectory(uploadpath);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* ɾ<EFBFBD><EFBFBD><EFBFBD>洢<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
* @param string
|
|||
|
|
*/
|
|||
|
|
public void deleteFolder(string path)
|
|||
|
|
{
|
|||
|
|
//if (Directory.Exists(path))
|
|||
|
|
//{
|
|||
|
|
// Directory.Delete(path, true);
|
|||
|
|
//}
|
|||
|
|
}
|
|||
|
|
}
|