代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
namespace EC
{
/// <summary>
/// 上传类
/// </summary>
public class UploadObj
{
public UploadObj()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 允许文件上传的类型枚举
/// </summary>
public enum FileType
{
jpg,gif,bmp,png
}
#region 取得文件后缀
/// <summary>
/// 取得文件后缀
/// </summary>
/// <param name="filename">文件名称</param>
/// <returns></returns>
public static string GetFileExtends(string filename)
{
string ext = null;
if (filename.IndexOf('.') > 0)
{
string[] fs = filename.Split('.');
ext = fs[fs.Length - 1];
}
return ext;
}
#endregion
#region 检测文件是否合法
/// <summary>
/// 检测上传文件是否合法
/// </summary>
/// <param name="fileExtends">文件后缀名</param>
/// <returns></returns>
public static bool CheckFileExtends(string fileExtends)
{
bool status = false;
fileExtends = fileExtends.ToLower();
string[] fe = Enum.GetNames(typeof(FileType));
for (int i = 0; i < fe.Length; i++)
{
if (fe[i].ToLower() == fileExtends)
{
status = true;
break;
}
}
return status;
}
#endregion
#region 保存文件
/// <summary>
/// 保存文件
/// </summary>
/// <param name="fpath">全路径,Server.MapPath()</param>
/// <param name="myFileUpload">上传控件</param>
/// <returns></returns>
public static string PhotoSave(string fpath,FileUpload myFileUpload)
{
string s = "";
string fileExtends = "";
string fileName = myFileUpload.FileName;
if (fileName != "")
{
//取得文件后缀
fileExtends = EC.UploadObj.GetFileExtends(fileName);
if (!EC.UploadObj.CheckFileExtends(fileExtends))
{
EC.MessageObject.ShowPre("上传文件类型不合法");
}
Random rd = new Random();
s = EC.RandomObject.DateRndName(rd) + "." + fileExtends;
string file = fpath + "//" + s;
try
{
myFileUpload.SaveAs(file);
}
catch (Exception ee)
{
throw new Exception(ee.ToString());
}
}
return s;
}
#endregion
#region 加入文字水印
/// <summary>
/// 加入文字水印
/// </summary>
/// <param name="fileName">文件名称路径(全路径)</param>
/// <param name="text">文件</param>
public void AddTextToImg(string fileName, string text)
新闻热点
疑难解答
图片精选