asp.net文件上传示例分享

  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

  {

  ///

  /// 上传类

  ///

  public class UploadObj

  {

  public UploadObj()

  {

  //

  // TODO: 在此处添加构造函数逻辑

  //

  }

  ///

  /// 允许文件上传的类型枚举

  ///

  public enum FileType

  {

  jpg,gif,bmp,png

  }

  #region 取得文件后缀

  ///

  /// 取得文件后缀

  ///

  /// 文件名称

  ///

  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 检测文件是否合法

  ///

  /// 检测上传文件是否合法

  ///

  /// 文件后缀名

  ///

  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 保存文件

  ///

  /// 保存文件

  ///

  /// 全路径,Server.MapPath()

  /// 上传控件

  ///

  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 加入文字水印

  ///

  /// 加入文字水印

  ///

  /// 文件名称路径(全路径)

  /// 文件

  public void AddTextToImg(string fileName, string text)

  {

  if (!File.Exists(fileName))

  {

  throw new FileNotFoundException("文件不存在");

  }

  if (text == string.Empty)

  {

  return;

  }

  System.Drawing.Image image = System.Drawing.Image.FromFile(fileName);

  Bitmap bitmap = new Bitmap(image, image.Width, image.Height);

  Graphics g = Graphics.FromImage(bitmap);

  float fontSize = 12.0f;//字体大小

  float textWidth = text.Length * fontSize;//文本的长度

  //下面定义一个矩形区域,以后在这个矩形里面画上白底黑字

  float rectX = 0;

  float rectY = 0;

  float rectWidth = text.Length * (fontSize + 8);

  float rectHeight = fontSize + 8;

  //声明矩形域

  RectangleF textArea = new RectangleF(rectX, rectY, rectWidth, rectHeight);

  Font font = new Font("宋体", fontSize);//定义字体

  Brush whiteBrush = new SolidBrush(Color.White);//白笔刷,画文字用

  Brush blackBrush = new SolidBrush(Color.Black);//黑笔刷,画背景用

  g.FillRectangle(blackBrush, rectX, rectY, rectWidth, rectHeight);

  g.DrawString(text, font, whiteBrush, textArea);

  MemoryStream ms = new MemoryStream();

  bitmap.Save(ms, ImageFormat.Jpeg);

  //输出处理后的图像,这里为了演示方便,我将图片显示在页面中了

  //Response.Clear();

  //Response.ContentType = "image/jpeg";

  //Response.BinaryWrite(ms.ToArray());

  g.Dispose();

  bitmap.Dispose();

  image.Dispose();

  }

  #endregion

  }

  }