简单的上传文件的webForm实例:
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileContentType = FileUpload1.PostedFile.ContentType;
if (fileContentType == "image/jpeg" || fileContentType == "image/gif" || fileContentType == "image/jpg")
{
string name = FileUpload1.PostedFile.FileName; // 客户端文件路径
FileInfo file = new FileInfo(name);
string fileName = file.Name; // 文件名称
string fileName_s = "x_" + file.Name; // 缩略图文件名称
string webFilePath = Server.MapPath("ImgUpload/" + fileName); // 服务器端文件路径
string webFilePath_s = Server.MapPath("ImgUpload/" + fileName_s); // 服务器端缩略图路径
if (!File.Exists(webFilePath))
{
try
{
FileUpload1.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件
MakeThumbnail(webFilePath, webFilePath_s, 130, 130); // 生成缩略图方法
Label1.Text = "提示:文件“" + fileName + "”成功上传,并生成“" + fileName_s + "”缩略图,文件类型为:" + FileUpload1.PostedFile.ContentType + ",文件大小为:" + FileUpload1.PostedFile.ContentLength + "B";
Image1.ImageUrl = webFilePath;
Image2.ImageUrl = webFilePath_s;
}
catch (Exception ex)
{
Label1.Text = "提示:文件上传失败,失败原因:" + ex.Message;
}
}
else
{
Label1.Text = "提示:文件已经存在";
}
}
else
{
Label1.Text = "提示:文件类型不符";
}
}
}
/**/
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel);
try
{
//以jpg格式保存缩略图
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string path = "C:\\Users\\J\\Desktop\\WebApplication1\\WebApplication1\\ImgUpload\\0.jpg";
string path2 = "C:\\Users\\J\\Desktop\\WebApplication1\\WebApplication1\\ImgUpload\\x_0.jpg";
if (File.Exists(path))
try
{
File.Delete(path);
File.Delete(path2);
}
catch
{
Label1.Text = "fail";
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "script language = 'javascript' defer>alert('删除失败');</script>");
}
else {
Label1.Text = "no file";
}
}
}