📄 upfile.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Imaging;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
namespace Ptym
{
/// <summary>
/// 图片信息的结构
/// </summary>
public struct Picinfo
{
/// <summary>
/// 新的宽度
/// </summary>
public UInt32 NewWidth;
/// <summary>
/// 新的文件名后面的后缀(如:_L || _B)
/// </summary>
public string BehandNewNameString;
/// <summary>
/// 用于生成图片后,返回不包括路径的新文件名
/// </summary>
public string NewFileName;
/// <summary>
/// 用于生成图片后,返回存入数据库的相对(根)路径,;
/// </summary>
public string NewFullName;
/// <summary>
/// 保存对要生成的图片的要求及保存生成后的路径及文件名
/// </summary>
/// <param name="newwidth">新图宽度大小</param>
/// <param name="filehouzhui">新图的文件名后缀</param>
public Picinfo(UInt32 newwidth,string filehouzhui)
{
NewWidth = newwidth;
BehandNewNameString = filehouzhui;
NewFileName = "";
NewFullName = "";
}
}
public class Uploadpic
{
#region 属性
/// <summary>
/// 图片信息列表
/// </summary>
public List<Picinfo> PicArray;
/// <summary>
/// 用于保存原图,为了方便的批生成几张规格的图片;
/// </summary>
private System.Drawing.Image oImage;
/// <summary>
/// 在本次保存时,唯一的图片名称前缀
/// </summary>
string onlyfilename;
public FileUpload Fileupload = null;
private string wulipath = "";
private string topath = @"Productsimage/";
/// <summary>
/// 上传到的目录地址,相对于根目录,不能有"\"或"~\";
/// </summary>
public string Topath
{
get
{
return topath;
}
set
{
topath =value;
}
}
private string limitfiletype=".jpg|.bmp|.gif|.png";
/// <summary>
/// 能上传的文件类型
/// </summary>
public string Limitfiletype
{
get
{
return limitfiletype;
}
set
{
limitfiletype = value;
}
}
private int limitfilesize=512000;
/// <summary>
/// 能上传的文件大小
/// </summary>
public int Limitfilesize
{
get
{
return limitfilesize;
}
set
{
limitfilesize = value;
}
}
private string oldfilename;
/// <summary>
/// 原上传的文件名称
/// </summary>
public string Oldfilename
{
get
{
return oldfilename;
}
set
{
oldfilename = value;
}
}
private string oldexe;
/// <summary>
/// 原上传文件的扩展名
/// </summary>
public string Oldexe
{
get
{
return oldexe;
}
set
{
oldexe = value;
}
}
#endregion
~Uploadpic()
{
try
{
this.Fileupload.Dispose();
PicArray = null;
oImage.Dispose();
}
catch
{
}
}
/// <summary>
/// 生成文件名(无后缀)
/// </summary>
private string Makefilename()
{
return DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
}
private bool Checkupfileisnull(FileUpload fileupload)
{
return fileupload.HasFile;
}
/// <summary>
/// 检查上传的文件路径是否存在,不存在就生成
/// </summary>
private void Checktopath()
{
if (!Directory.Exists(wulipath))
{
Directory.CreateDirectory(wulipath);
}
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="fileuploadcontrol">真正的上传文件的控件</param>
public Uploadpic(FileUpload fileuploadcontrol)
{
Fileupload = fileuploadcontrol;
PicArray = new List<Picinfo>();
}
/// <summary>
/// 检查上传文件格式
/// </summary>
private bool Checkexe(string fullname)
{
oldexe= Path.GetExtension(fullname).ToLower();
string[] allexe =limitfiletype.Split('|');
foreach (string x in allexe)
{
if (oldexe==x)
{
return true;
}
}
return false;
}
/// <summary>
/// 检查是否上传的图片
/// </summary>
/// <returns></returns>
private bool Checkisimage()
{
if (!Fileupload.PostedFile.ContentType.StartsWith("image"))
{
return false;
}
return true;
}
/// <summary>
/// 检查topath的后面有没有"/",没有就加上,还有前缀看对不对,不能以"\"开头
/// </summary>
/// <returns></returns>
private string Checkctopathformat(string path)
{
string returnpath = path;
if (!returnpath.EndsWith(@"/"))
{
returnpath+=@"/";
}
if(returnpath.StartsWith(@"/"))
{
returnpath=returnpath.Remove(0,1);
}
if (returnpath.StartsWith(@"~\"))
{
returnpath = returnpath.Remove(0, 2);
}
wulipath = HttpContext.Current.Server.MapPath(@"~\"+topath);
return returnpath;
}
/// <summary>
/// 生成原图
/// </summary>
/// <param name="UploadFile"></param>
/// <returns></returns>
private System.Drawing.Image Makesourcepic(FileUpload UploadFile)
{
Byte[] oFileByte = new byte[Fileupload.PostedFile.ContentLength];
System.IO.Stream oStream = Fileupload.PostedFile.InputStream;
System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
oStream.Dispose();
return oImage;
}
/// <summary>
/// 按比例计算出缩略图的宽度和高度
/// </summary>
/// <param name="oWidth"></param>
/// <param name="tWidth"></param>
/// <returns></returns>
private Point Caculatenewwidth(int oWidth, int oHeight,int tWidth)
{
int tHeight=0;
tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
return new Point(tWidth, tHeight);
}
/// <summary>
/// 获取源文件的格式
/// </summary>
/// <returns></returns>
private System.Drawing.Imaging.ImageFormat Getfileformat(string _oldexe )
{
if (_oldexe == ".gif")
return System.Drawing.Imaging.ImageFormat.Gif;
if (_oldexe == ".jpg" || _oldexe == ".jpge")
return System.Drawing.Imaging.ImageFormat.Jpeg;
if (_oldexe == ".png")
return System.Drawing.Imaging.ImageFormat.Png;
if (_oldexe == ".bmp")
return System.Drawing.Imaging.ImageFormat.Bmp;
return System.Drawing.Imaging.ImageFormat.Jpeg;
}
/// <summary>
/// 保存图片到指定地址(及文件名)
/// </summary>
/// <param name="images">bitmap格式的图像</param>
/// <param name="path">保存地址及文件名</param>
/// <returns></returns>
private bool Savepic(Bitmap images,string path)
{
try
{
images.Save(path, Getfileformat(oldexe));
}
catch
{
return false;
}
return true;
}
/// <summary>
/// 生成并保存指定大小的文件
/// </summary>
/// <param name="image">源图</param>
/// <param name="tpoint">要达到的长宽</param>
/// <param name="opoint">源图的长宽</param>
/// <param name="fullfilename">保存的详细文件名及路径</param>
/// <returns>成功为真</returns>
private bool Makepic(System.Drawing.Image image, Point tpoint, Point opoint, string fullfilename)
{
int tWidth = tpoint.X;
int tHeight = tpoint.Y;
int oWidth = opoint.X;
int oHeight = opoint.Y;
try
{
System.Drawing.Image oImage = image;
//生成缩略原图
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);
Savepic(tImage, fullfilename);
//释放资源
g.Dispose();
tImage.Dispose();
}
catch
{
return false;
}
return true;
}
/// <summary>
/// 正式开始上传前再检查一下上传的相关参数是否正常
/// </summary>
/// <returns></returns>
private bool Befortoupload()
{
//是否有上传文件
if (!Checkupfileisnull(Fileupload))
{
return false;
}
if(!Checkisimage())
{
return false;
}
//检查文件大小
if (Fileupload.PostedFile.ContentLength > limitfilesize)
{
return false;
}
//上传格式是否正确
if (!Checkexe(Fileupload.PostedFile.FileName))
{
return false;
}
//检查文件名是否正确
topath = Checkctopathformat(topath);
//检查上传路径是否存在
try
{
Checktopath();
}
catch
{
return false;
}
oldfilename = Fileupload.FileName;
onlyfilename = Makefilename();
//生成源图
oImage = Makesourcepic(Fileupload);
return true;
}
/// <summary>
/// 最后上传完后清理原图
/// </summary>
private void Endupload()
{
try
{
oImage.Dispose();
}
catch
{
}
}
/// <summary>
/// 上传文件
/// </summary>
/// <returns></returns>
private bool Upload(ref Picinfo picinfo)
{
//上传文件
try
{
picinfo.NewFileName= onlyfilename + picinfo.BehandNewNameString + oldexe;
picinfo.NewFullName = topath + picinfo.NewFileName;
Point tPoint;
//此参数为0,表示不改变源图大小
if (picinfo.NewWidth > 0)
{
tPoint = Caculatenewwidth(oImage.Width, oImage.Height, Convert.ToInt32(picinfo.NewWidth));
}
else
{
tPoint = new Point(oImage.Width, oImage.Height);
}
Makepic(oImage, tPoint, new Point(oImage.Width, oImage.Height), wulipath + onlyfilename + picinfo.BehandNewNameString + oldexe);
}
catch
{
oImage.Dispose();
return false;
}
return true;
}
/// <summary>
/// 两种保存方式:1:自动读取picarray的值,并按其设置进行保存2:仅保存源图,且不改变其大小
/// </summary>
/// <returns></returns>
public bool Save()
{
if(PicArray.Count==0)
{
this.PicArray.Add(new Picinfo(0,""));
}
return DoSave();
}
/// <summary>
/// 保存源图,并指定新大小;
/// </summary>
/// <param name="newwidth"></param>
/// <returns></returns>
public bool Save(UInt32 newwidth)
{
PicArray.Clear();
PicArray.Add(new Picinfo(newwidth, ""));
return DoSave();
}
/// <summary>
/// 按picinfo中指定的信息进行保存(可以保存多种宽度的图片)
/// </summary>
private bool DoSave()
{
if (PicArray.Count == 0)
{
//没有指定picinfo信息
return false;
}
if (!Befortoupload()) { return false; }
for (int i = 0; i < PicArray.Count; i++)
{
Picinfo x=PicArray[i];
if (!Upload(ref x)) { return false; }
PicArray[i] = x;
}
Endupload();
return true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -