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

  • 相关阅读:
    centos7开启关闭防火墙
    虚拟机vmnet1和vmnet8找不到,注册表没有删除干净,见图
    dhcp和static的区别
    SQL手册
    canal架构原理
    SQL优化(待完善)
    数仓简介
    java逆变与协变(待完善)
    mysqljoin的原理和优化
    深入理解java虚拟机阅读笔记
  • 原文地址:https://www.cnblogs.com/handboy/p/7158349.html
Copyright © 2011-2022 走看看