258 lines
8.9 KiB
C#
258 lines
8.9 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Web;
|
||
|
||
using System.IO;
|
||
using System.Net;
|
||
using SuperMap.RealEstate.Utility;
|
||
|
||
namespace SuperMap.RealEstate.HousingSecurity.APIPlugs.flashuploader
|
||
{
|
||
public class MyUpFileInfo
|
||
{
|
||
//hzz:
|
||
//转换 HttpPostedFileBase filebase = new HttpPostedFileWrapper(HttpPostedFile);
|
||
//
|
||
private HttpPostedFileBase _httpPostedFile;
|
||
public HttpPostedFileBase HttpPostedFileBase
|
||
{
|
||
get { return _httpPostedFile; }
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 直接用对应数据库的表进行构造,方便处理
|
||
/// </summary>
|
||
/// <param name="entity"></param>
|
||
public MyUpFileInfo(t_upfilesInfo entity)
|
||
{
|
||
_Entity = entity;
|
||
}
|
||
|
||
//public UpFileInfo(HttpPostedFileBase postfile, int userid)
|
||
//{
|
||
// string _md5 = GetMD5_32(postfile.InputStream);
|
||
// string _ext = Path.GetExtension(postfile.FileName).ToLower();
|
||
// string _usergroup = GetMD5_32(userid.ToString()).Substring(0, 2); //按用户HASH分组(00-ff)
|
||
|
||
// _Entity = new t_upfilesInfo();
|
||
// _Entity.md5 = _md5;
|
||
// _Entity.fileext = _ext;
|
||
// _Entity.mimetype = postfile.ContentType;
|
||
// _Entity.filesize = postfile.ContentLength;
|
||
// _Entity.filename = postfile.FileName;
|
||
// //Init(postfile, userid);
|
||
//}
|
||
public MyUpFileInfo(HttpPostedFileBase postfilebase, int userid, bool isnews = false)
|
||
{
|
||
Init(postfilebase, userid, isnews);
|
||
}
|
||
public MyUpFileInfo(HttpPostedFile postfile, int userid, bool isnews = false)
|
||
{
|
||
Init(new HttpPostedFileWrapper(postfile), userid, isnews);
|
||
}
|
||
public MyUpFileInfo(HttpWebResponse webfile, byte[] bytes, int userid)
|
||
{
|
||
Init(webfile, bytes, userid);
|
||
}
|
||
|
||
private static string[] filetype_img = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" };
|
||
private static string[] filetype_file = { ".rar", ".doc", ".docx", ".zip", ".pdf", ".txt" };
|
||
private static string[] filetype_flv = { ".swf", ".mp3", ".mp4", ".flv", ".wmv", ".rmvb", ".mkv" };
|
||
|
||
|
||
public void Init(HttpPostedFileBase postfile, int userid, bool isnews = false)
|
||
{
|
||
this._httpPostedFile = postfile;
|
||
|
||
string _md5 = GetMD5_32(postfile.InputStream);
|
||
string _ext = Path.GetExtension(postfile.FileName).ToLower();
|
||
string _usergroup = userid.ToEncrypt(); //GetMD5_32(userid.ToString());//.Substring(0, 2); //按用户HASH分组(00-ff)
|
||
|
||
_Entity = new t_upfilesInfo();
|
||
_Entity.md5 = _md5;
|
||
_Entity.filetime = DateTime.Now.ToFileTime().ToString();
|
||
_Entity.fileext = _ext;
|
||
_Entity.mimetype = postfile.ContentType;
|
||
_Entity.filesize = postfile.ContentLength;
|
||
_Entity.filename = postfile.FileName;
|
||
//_Entity.tags = postfile.ContentType;
|
||
//_Entity.intro = postfile.ContentType;
|
||
|
||
//服务端路径和文件名设定
|
||
_Entity.serverfilename = _Entity.filetime + _ext;
|
||
//_Entity.serverfilename = _md5 + _ext;
|
||
|
||
|
||
if (isnews)
|
||
{
|
||
//新闻目录
|
||
if (filetype_img.Contains(_ext))
|
||
{
|
||
_Entity.serverpath = string.Format("/Temporary/HousingSecurity/news/img/");
|
||
}
|
||
else if (filetype_flv.Contains(_ext))
|
||
{
|
||
_Entity.serverpath = string.Format("/Temporary/HousingSecurity/news/flv/");
|
||
}
|
||
else
|
||
{
|
||
_Entity.serverpath = string.Format("/Temporary/HousingSecurity/news/file/");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//统一的文件目录(用于房源图片)
|
||
if (filetype_img.Contains(_ext))
|
||
{
|
||
_Entity.serverpath = string.Format("/Temporary/HousingSecurity/img/{0}/", _usergroup);
|
||
}
|
||
else if (filetype_flv.Contains(_ext))
|
||
{
|
||
_Entity.serverpath = string.Format("/Temporary/HousingSecurity/flv/{0}/", _usergroup);
|
||
}
|
||
else
|
||
{
|
||
_Entity.serverpath = string.Format("/Temporary/HousingSecurity/file/{0}/", _usergroup);
|
||
}
|
||
}
|
||
|
||
_Entity.serverAllPhysicalPath = ServerFullSavePathAndFileNameExt;
|
||
|
||
}
|
||
|
||
public void Init(HttpWebResponse webfile, byte[] bytes, int userid)
|
||
{
|
||
string _remoteUrl = webfile.ResponseUri.ToString();
|
||
string _md5 = GetMD5_32(new MemoryStream(bytes));
|
||
string _ext = Path.GetExtension(_remoteUrl).ToLower();
|
||
string _usergroup = userid.ToEncrypt();//GetMD5_32(userid.ToString()); //按用户HASH分组(00-ff)
|
||
|
||
_Entity = new t_upfilesInfo();
|
||
|
||
_Entity.md5 = _md5;
|
||
_Entity.filetime = DateTime.Now.ToFileTime().ToString();
|
||
_Entity.fileext = _ext;
|
||
_Entity.mimetype = webfile.ContentType;
|
||
_Entity.filesize = Convert.ToInt32(webfile.ContentLength);
|
||
_Entity.filename = Path.GetFileName(_remoteUrl);
|
||
|
||
//服务端路径和文件名设定
|
||
_Entity.serverfilename = _Entity.filetime + _ext;
|
||
_Entity.serverpath = string.Format("/Temporary/HousingSecurity/web/{0}/", _usergroup);
|
||
}
|
||
|
||
public string GetUri()
|
||
{
|
||
//int fid
|
||
return (this._Entity.serverpath + _Entity.filetime + _Entity.fileext).Replace("\\", "/");
|
||
}
|
||
|
||
private t_upfilesInfo _Entity;
|
||
/// <summary>
|
||
/// 数据库对应的文件记录信息
|
||
/// </summary>
|
||
public t_upfilesInfo Entity
|
||
{
|
||
get { return _Entity; }
|
||
set { _Entity = value; }
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 服务器完整物理路径,包含盘符(仅完整物理路径)
|
||
/// <para>格式:E:\01_MyWorks\left9_2013\src\Hzz.WebSite\upload\img\201306\c4\</para>
|
||
/// <para></para>
|
||
/// </summary>
|
||
public string ServerFullSavePath
|
||
{
|
||
get
|
||
{
|
||
return System.Web.HttpContext.Current.Server.MapPath("~" + _Entity.serverpath);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 服务器完整存储路径(完整物理路径+文件名)
|
||
/// <para>格式:E:\01_MyWorks\left9_2013\src\Hzz.WebSite\upload\img\201306\c4\0f28df94d0f9cd42.jpg</para>
|
||
/// </summary>
|
||
public string ServerFullSavePathAndFileNameExt
|
||
{
|
||
get
|
||
{
|
||
return ServerFullSavePath + ServerSaveFileName;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 服务器保存路径(相对路径)
|
||
/// <para>格式:/upload/img/201306/c4/</para>
|
||
/// </summary>
|
||
public string ServerSavePath
|
||
{
|
||
get
|
||
{
|
||
return _Entity.serverpath;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 服务器保存文件名(md5 hash后基本唯一)
|
||
/// <para>0f28df94d0f9cd42.jpg</para>
|
||
/// </summary>
|
||
public string ServerSaveFileName
|
||
{
|
||
get
|
||
{
|
||
return _Entity.serverfilename;
|
||
}
|
||
}
|
||
|
||
///// <summary>
|
||
///// 访问的参考URL
|
||
///// <para>格式:/upload/img/{00-ff}/{md5}.abc</para>
|
||
///// </summary>
|
||
//public string PreviewUri
|
||
//{
|
||
// get
|
||
// {
|
||
// return this._Entity.serverpath + _Entity.md5 + _Entity.fileext;
|
||
// }
|
||
//}
|
||
|
||
///// <summary>
|
||
///// 新的文件名+文件后缀
|
||
///// </summary>
|
||
//public string Uri_FileName
|
||
//{
|
||
// get
|
||
// {
|
||
// return _Entity.md5 + _Entity.fileext;
|
||
// }
|
||
//}
|
||
|
||
|
||
private static string GetMD5_32(Stream stream)
|
||
{
|
||
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
|
||
string resule = BitConverter.ToString(md5.ComputeHash(stream));
|
||
return resule.Replace("-", "").ToLower();
|
||
}
|
||
private static string GetMD5_32(string str)
|
||
{
|
||
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
|
||
string resule = BitConverter.ToString(md5.ComputeHash(System.Text.UTF8Encoding.Default.GetBytes(str)));
|
||
return resule.Replace("-", "").ToLower();
|
||
}
|
||
|
||
public byte[] GetPicBlob(string PicPath)
|
||
{
|
||
FileStream fs = new FileStream(PicPath, FileMode.Open);
|
||
byte[] byDate = new byte[fs.Length];
|
||
fs.Read(byDate, 0, byDate.Length);
|
||
fs.Close();
|
||
return byDate;
|
||
}
|
||
}
|
||
} |