コード例 #1
0
    /// <summary>
    /// 文件上传
    /// </summary>
    /// <param name="UploadFile">文件上传控件</param>
    /// <returns>返回是否成功保存图片</returns>
    public bool UpLoadFile(HttpPostedFile UploadFile)
    {
        try
        {
            if (UploadFile != null)//检查是否已经选择文件
            {
                string filename = UploadFile.FileName.ToLower();
                TFileName = filename;
                int i = filename.LastIndexOf(".");
                filename = filename.Substring(i, filename.Length - i);

                //格式化日期作为文件名
                string oStringTime = ObjectFormatUtil.GetNumByDateTime(4);
                string PathName    = ObjectFormatUtil.GetNumByDateTime(0);
                OFileName = PathName + filename;
                string oSavePath = HttpContext.Current.Server.MapPath("~") + "\\" + Path + "\\" + oStringTime + "\\";
                if (!Directory.Exists(oSavePath))
                {
                    Directory.CreateDirectory(oSavePath);//在根目录下建立文件夹
                }

                if (OldName != "")
                {
                    string full_name = HttpContext.Current.Server.MapPath("~" + OldName);
                    if (File.Exists(full_name))
                    {
                        File.Delete(full_name);
                    }
                }

                //保存路径+完整文件名
                OFullName = oSavePath + OFileName;
                FileName  = url + "/" + Path + "/" + oStringTime + "/" + OFileName;
                UploadFile.SaveAs(OFullName);
                MSG = "图片文件成功!";
                return(true);
            }
            else
            {
                MSG = "";
                return(true);
            }
        }
        catch (Exception ex)
        {
            MSG = "上传文件异常:" + ex.Message;
            return(false);
        }
    }
コード例 #2
0
    /// <summary>
    /// 图片上传(默认:"等比压缩,限定上传尺寸2048*1536,缩略图尺寸100*100,限定上传大小1MB,存放在根目录UpdateFile中")
    /// </summary>
    /// <param name="UploadFile">文件上传控件</param>
    /// <returns>返回是否成功保存图片</returns>
    public bool UpLoadIMG(HttpPostedFile UploadFile)
    {
        try
        {
            if (UploadFile != null)//检查是否已经选择文件
            {
                string filename = UploadFile.FileName.ToLower();
                int    i        = filename.LastIndexOf(".");
                filename = filename.Substring(i, filename.Length - i);
                if (!(filename == ".jpg" || filename == ".jpeg" || filename == ".gif" || filename == ".png" || filename == ".bmp"))
                {
                    UpLoadFile(UploadFile);
                    return(true);
                }
                //检查上传文件的格式是否有效
                //if (UploadFile.ContentLength == 0 || UploadFile.ContentLength >= Size)
                //{
                //    MSG = "指定的文件大小不符合要求,限定上传大小1MB!";
                //    return false;
                //}
                //检查图片文件的大小
                //生成原图
                Stream oStream = UploadFile.InputStream;
                System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
                int owidth  = oImage.Width;  //原图宽度
                int oheight = oImage.Height; //原图高度
                //if (owidth > LimitWidth || oheight > LimitHeight)
                //{
                //    MSG = "超过允许的图片尺寸范围,限定上传尺寸" + LimitWidth + "*" + LimitHeight + "!";
                //    return false;
                //}
                //检查是否超出规定尺寸
                if (IsRate)
                {
                    //按比例计算出缩略图的宽度和高度
                    if (owidth >= oheight)
                    {
                        THeight = (int)Math.Floor(Convert.ToDouble(oheight) * (Convert.ToDouble(TWidth) / Convert.ToDouble(owidth)));//等比设定高度
                    }
                    else
                    {
                        TWidth = (int)Math.Floor(Convert.ToDouble(owidth) * (Convert.ToDouble(THeight) / Convert.ToDouble(oheight)));//等比设定宽度
                    }
                }
                //生成缩略原图
                Bitmap   tImage = new Bitmap(TWidth, THeight);
                Graphics g      = Graphics.FromImage(tImage);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;    //设置高质量插值法
                g.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //设置高质量,低速度呈现平滑程度
                g.Clear(Color.Transparent);                                               //清空画布并以透明背景色填充
                g.DrawImage(oImage, new Rectangle(0, 0, TWidth, THeight), new Rectangle(0, 0, owidth, oheight), GraphicsUnit.Pixel);

                //格式化日期作为文件名
                string oStringTime = ObjectFormatUtil.GetNumByDateTime(4);
                string PathName    = ObjectFormatUtil.GetNumByDateTime(0);
                OFileName = PathName + filename;
                TFileName = PathName + filename.Replace(".", "_t.");
                string oSavePath = HttpContext.Current.Server.MapPath("~") + "\\" + Path + "\\" + oStringTime + "\\";
                if (!Directory.Exists(oSavePath))
                {
                    Directory.CreateDirectory(oSavePath);//在根目录下建立文件夹
                }

                if (OldName != "")
                {
                    string full_name = HttpContext.Current.Server.MapPath("~" + OldName);
                    if (File.Exists(full_name))
                    {
                        File.Delete(full_name);
                    }
                    full_name = HttpContext.Current.Server.MapPath("~" + OldName.Replace(".", "_t."));
                    if (File.Exists(full_name))
                    {
                        File.Delete(full_name);
                    }
                }

                //保存路径+完整文件名
                OFullName = oSavePath + OFileName;
                TFullName = oSavePath + TFileName;
                FileName  = url + "/" + Path + "/" + oStringTime + "/" + OFileName;
                //开始保存图片至服务器
                try
                {
                    switch (filename)
                    {
                    case ".jpeg":
                    case ".jpg":
                    {
                        oImage.Save(OFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
                        tImage.Save(TFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
                        break;
                    }

                    case ".gif":
                    {
                        oImage.Save(OFullName, System.Drawing.Imaging.ImageFormat.Gif);
                        tImage.Save(TFullName, System.Drawing.Imaging.ImageFormat.Gif);
                        break;
                    }

                    case ".png":
                    {
                        oImage.Save(OFullName, System.Drawing.Imaging.ImageFormat.Png);
                        tImage.Save(TFullName, System.Drawing.Imaging.ImageFormat.Png);
                        break;
                    }

                    case ".bmp":
                    {
                        oImage.Save(OFullName, System.Drawing.Imaging.ImageFormat.Bmp);
                        tImage.Save(TFullName, System.Drawing.Imaging.ImageFormat.Bmp);
                        break;
                    }
                    }
                    MSG = "图片上传成功!";
                    return(true);
                }
                catch (Exception ex)
                {
                    MSG = ex.Message;
                    return(false);
                }
                finally
                {
                    //释放资源
                    oImage.Dispose();
                    g.Dispose();
                    tImage.Dispose();
                }
            }
            else
            {
                MSG = "";
                return(true);
            }
        }
        catch (Exception ex)
        {
            MSG = "上传图片异常:" + ex.Message;
            return(false);
        }
    }