1. 程式人生 > >圖片上傳封裝類【包括圖片上傳和縮略圖上傳】.NET

圖片上傳封裝類【包括圖片上傳和縮略圖上傳】.NET

tolower tdi p s else col width posted ef6 substr

原文發布時間為:2009-08-30 —— 來源於本人的百度文章 [由搬家工具導入]

#region 上传图片及上传缩略图
    public class UpFile : System.Web.UI.Page
    {

        /// <summary>
        /// 上传图片jpg/jpeg/gif/bmp格式
        /// 创建:2009-06-09
        /// 作者:HandBoy
        /// </summary>

        private string fileName = String.Empty;
        public string FileName
        {
            get { return fileName; }
            set { fileName = value; }
        }

        /// <summary>
        /// 上传图片,需设置上传目录
        /// </summary>
        /// <param name="postFile">FileUpload1.PostedFile</param>
        /// <param name="postDir">上传的目录。如:"Upload"</param>
        ///
        /// 调用例子: WebPublic.UpFile uf = new WebPublic.UpFile();
        ///             uf.upFile(FileUpload1.PostedFile, "Upload",150,120);
        ///            
        public void upFile(HttpPostedFile postFile, string postDir, int width, int height)
        {
            if (postFile.FileName.Length > 0)
            {
                int fileLength = postFile.ContentLength;
                if (fileLength / 1024 <= 500)
                {
                    string fileContentType = postFile.ContentType;
                    if (fileContentType == "image/pjpeg" || fileContentType == "image/gif" || fileContentType == "image/bmp")
                    {
                        //图片重命名
                        string nowStr = DateTime.Now.ToString("yyyyMMddHHmmss");
                        //获得文件扩展名
                        string fileType = postFile.FileName.Substring(postFile.FileName.LastIndexOf(".")).ToLower();
                        if (fileType != ".gif")
                            fileType = ".jpg";
                        fileName = nowStr + fileType;
                        //上传图片路径
                        string dir = Server.MapPath(postDir);
                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }
                        string savePath = dir + "/" + fileName;
                        if (File.Exists(savePath))
                        {
                            alert("此时上传图片人数过多上传失败,请重试!");
                        }
                        else
                        {
                            postFile.SaveAs(savePath);
                            if (width != 0 && height != 0)
                            {
                                string minPath = dir + "/min_" + fileName;
                                Image img = Image.FromFile(savePath);
                                System.Drawing.Bitmap bmp = new Bitmap(img, width, height);
                                bmp.Save(minPath);
                                bmp.Dispose();
                                img.Dispose();
                            }
                            alert("图片上传成功!");
                        }
                    }
                    else
                    {
                        alert("图片格式不正确!");
                    }
                }
                else
                {
                        alert("您要上传的图片太大请上传500K以内的图片!");
                }
            }
            else
            {
                alert("请选择图片!");
            }
        }

        // <summary>
        /// 上传图片,默认目录为Upload
        /// </summary>
        ///
        /// 调用例子: WebPublic.UpFile uf = new WebPublic.UpFile();
        ///             uf.upFile(FileUpload1.PostedFile);
        ///            
        public void upFile(HttpPostedFile postFile)
        {
            upFile(postFile, "Upload", 0, 0);
        }

        ///<summary>
        /// 上传图片及缩略图,默认目录为Upload
        /// </summary>
        ///
        /// WebPublic.UpFile uf = new WebPublic.UpFile();
        /// uf.upFile(FileUpload1.PostedFile, 150,120);
        ///
        public void upFile(HttpPostedFile postFile, int width, int height)
        {
            upFile(postFile, "Upload", width, height);
        }

        #region 提示
        public static void alert(string strAlert)
        {
            string temp;
            temp = "<Script language='javascript'>";
            temp += "alert('" + strAlert + "');";
            temp += "</Script>";
            System.Web.HttpContext.Current.Response.Write(temp);
        }
       #endregion
    }
    #endregion

圖片上傳封裝類【包括圖片上傳和縮略圖上傳】.NET