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;

/// <summary>
/// UploadFilePic 的摘要说明
/// </summary>
public class UploadFilePic

    string  picType="";
    private string dirUrl="";
    private string NewFileName = "";
    private int fileLength = 0;
    string weburl="";
 public UploadFilePic()
 {
  //
  // TODO: 在此处添加构造函数逻辑
  //
 }
    /// <summary>
    /// 设置图片格式以|分割
    /// </summary>
    public string _PicType
    {
        set
        {

            picType = value;
            if (picType == "")
            {
                picType = "JPEG";
            }
        }
    }
    public string _dirUrl {
        set {
            dirUrl = value;
        }
    }
    public string _weburl
    {
        get
        {
            return weburl;
        }
        set
        {
            weburl = value;
        }
    }
 
    /// <summary>
    /// 上传文件的大小,以 Kb为单位
    /// </summary>
    public int FileLength
    {
        get
        {
            if (fileLength != 0) return fileLength;
            return 1024;
        }
        set
        {
            fileLength = value * 1024;
        }
    }

    /// <summary>
    /// 新文件名称
    /// </summary>
    public string _NewFileName
    {
        get
        {
            return NewFileName;
        }
        set
        {
            NewFileName = value;
        }
    }

    public void UploadPic(FileUpload FileUpload1)
    {
      
            string filename;//文件名字
            string hz;
            string newname;
            int lenpic =0;//图片大小
            filename = FileUpload1.FileName;
            lenpic = FileUpload1.PostedFile.ContentLength;
            string FileType=FileUpload1.PostedFile.ContentType;
            FileType = FileType.Substring(0, FileType.IndexOf("/"));
            if (FileType.CompareTo("image") != 0) {
                HttpContext.Current.Response.Write("<script>alert('图片只支持---" + picType + "');history.back();</script>");
                HttpContext.Current.Response.End();
            } 
            if (lenpic > fileLength) {
               HttpContext.Current.Response.Write("<script>alert('图片大小超出范围,只允许上传的大小为"+fileLength +"kb');history.back();</script>");
               HttpContext.Current.Response.End();
            }
            //取后最
            int pos = filename.IndexOf(".");
            hz = filename.Substring((pos + 1)).ToUpper();
            string[] Type_pic = picType.Split('|');
            bool yes =false;
            if (Type_pic != null)
            {

                for (int i = 0; i < Type_pic.Length; ++i)
                {

                    if (hz.CompareTo(Type_pic[i].ToUpper()) == 0)
                    {

                        yes = true;
                        break;
                    }

                }
                if (yes == false)
                {
                    HttpContext.Current.Response.Write("<script>alert('图片只支持---" + picType + "');history.back();</script>");
                    HttpContext.Current.Response.End();
                }
            }
          
            newname = sjname() + "." + hz;
            NewFileName = weburl + newname;
            //保存图片
            string pathurl;
            pathurl = HttpContext.Current.Server.MapPath("~") + dirUrl+newname;
            string kb = FileUpload1.FileContent.Length.ToString();

            FileUpload1.PostedFile.SaveAs(pathurl);

    }
 
    /// <summary>
    /// 产生个随即名称
    /// </summary>
    /// <returns></returns>
    public string sjname()
    {

        string sj = null;
        sj = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.TimeOfDay.Hours.ToString() + DateTime.Now.TimeOfDay.Minutes.ToString() + DateTime.Now.TimeOfDay.Milliseconds.ToString();
        return sj;


    }
}
public enum pictype
{
    gif = 0,
    jpg = 1,
    jpeg = 2,
    bmp = 3,
    swf = 4,

}

 

 

你可能感兴趣的:(ASP.NET上传图片类)