zoukankan      html  css  js  c++  java
  • 图片上传封装类【包括图片上传和缩略图上传】.NET

    原文发布时间为: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

  • 相关阅读:
    分布式系统笔记
    Paxos算法细节详解(一)
    Java G1学习笔记
    Spring Boot 的 10 个核心模块
    k8s 重点
    毕玄:阿里十年,从分布式到云时代的架构演进之路
    netty原理解析
    JVM调优总结(一):基本概念
    《快学 Go 语言》第 16 课 —— 包管理 GOPATH 和 Vendor
    Oracle 检查表空间使用情况
  • 原文地址:https://www.cnblogs.com/handboy/p/7158349.html
Copyright © 2011-2022 走看看