您的位置:首页 > 编程语言 > ASP

上传 文件类型判断 缩略图实现

2009-09-22 10:16 465 查看
页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="image.aspx.cs" Inherits="image" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="UploadFile" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="上传" />
<br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />
<br />
</div>
</form>
</body>
</html>


后台:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.IO;

public partial class image : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
//检查上传文件的格式是否有效

if (this.UploadFile.PostedFile.ContentType.ToLower().IndexOf("image") < 0)
{
Response.Write("上传图片格式无效!");
return;
}

//生成原图
Byte[] oFileByte = new byte[this.UploadFile.PostedFile.ContentLength];
System.IO.Stream oStream = this.UploadFile.PostedFile.InputStream;
System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);

int oWidth = oImage.Width; //原图宽度
int oHeight = oImage.Height; //原图高度
int tWidth = 200; //设置缩略图初始宽度
int tHeight = 200; //设置缩略图初始高度

//按比例计算出缩略图的宽度和高度
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 oFullName = Server.MapPath(".") + "/image/" + "o" + DateTime.Now.ToShortDateString().Replace("-", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg"; //保存原图的物理路径
string tFullName = Server.MapPath(".") + "/image/" + "t" + DateTime.Now.ToShortDateString().Replace("-", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg"; //保存缩略图的物理路径

try
{
//以JPG格式保存图片
string urlfile=Server.MapPath(".") + "/image/";
if( !Directory.Exists( urlfile))
Directory.CreateDirectory(urlfile);
oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
tImage.Save(tFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
throw ex;
}
finally
{
//释放资源
oImage.Dispose();
g.Dispose();
tImage.Dispose();

}

}

protected void Button2_Click(object sender, EventArgs e)
{

FileExtension[] fe = { FileExtension.GIF, FileExtension.JPG, FileExtension.PNG };
if (!FileValidation.IsAllowedExtension(FileUpload1, fe))
{

Response.Write("文件格式有误");
return;
}

string filename = this.FileUpload1.PostedFile.FileName;
string savepath = Server.MapPath(".") + "/upload/";
if (!Directory.Exists(savepath))
Directory.CreateDirectory(savepath);

string UploadFileExecName = filename.Substring(filename.LastIndexOf(".") + 1);//得到文件的扩展名
Random rd = new Random();//产生随机数
int valationNo = 10 + rd.Next(99);//产生随机数
string suiji = valationNo.ToString();//产生随机数
string UpLoadFileTime = DateTime.Now.ToString("yyyyMMHHmmss") + suiji;//得到系统时间并加上随机数以便生成上传图片名称
string NewUploadFileName = UpLoadFileTime + "." + UploadFileExecName;//产生上传图片的名称

this.FileUpload1.PostedFile.SaveAs(savepath +  NewUploadFileName);
}

public enum FileExtension
{
JPG = 255216,
GIF = 7173,
PNG = 13780,
SWF = 6787,
RAR = 8297,
ZIP = 8075,
_7Z = 55122

// 255216 jpg;

// 7173 gif;

// 6677 bmp,

// 13780 png;

// 6787 swf

// 7790 exe dll,

// 8297 rar

// 8075 zip

// 55122 7z

// 6063 xml

// 6033 html

// 239187 aspx

// 117115 cs

// 119105 js

// 102100 txt

// 255254 sql

}

public class FileValidation
{
public static bool IsAllowedExtension(FileUpload fu, FileExtension[] fileEx)
{
int fileLen = fu.PostedFile.ContentLength;
byte[] imgArray = new byte[fileLen];
fu.PostedFile.InputStream.Read(imgArray, 0, fileLen);
MemoryStream ms = new MemoryStream(imgArray);
System.IO.BinaryReader br = new System.IO.BinaryReader(ms);
string fileclass = "";
byte buffer;
try
{
buffer = br.ReadByte();
fileclass = buffer.ToString();
buffer = br.ReadByte();
fileclass += buffer.ToString();
}
catch
{
}
br.Close();
ms.Close();
foreach (FileExtension fe in fileEx)
{
if (Int32.Parse(fileclass) == (int)fe)
return true;
}
return false;
}
}

}
  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息