前臺(tái)代碼:
復(fù)制代碼 代碼如下:
%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="upload" %>
!DOCTYPE html>
html xmlns="http://www.w3.org/1999/xhtml">
head runat="server">
meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
title>/title>
style type="text/css">
li
{
list-style: none;
padding-top: 10px;
}
/style>
script type="text/javascript" src="js/jquery-1.6.2.min.js">/script>
script type="text/javascript">
function ValidImage(id, msg) {
$(id).parent().append("span>" + msg + "/span>");
return false;
}
/script>
/head>
body>
form id="form1" runat="server" enctype="multipart/form-data" method="post">
div>
ul>
li>
input type="file" id="upload1" name="upload" />
/li>
li>
input type="file" id="upload2" name="upload" />
/li>
li>
input type="file" id="upload3" name="upload" />
/li>
li>
input type="file" id="upload4" name="upload" />/li>
li>
input type="file" id="upload5" name="upload" />
/li>
li>
input type="submit" id="btnPostFile" runat="server" onserverclick="btnPostFile_ServerClick" value="開(kāi)始上傳" />
/li>
/ul>
/div>
/form>
/body>
/html>
前臺(tái)就是幾個(gè)控件和一個(gè)ValidImage方法。
后臺(tái)代碼:
復(fù)制代碼 代碼如下:
protected void btnPostFile_ServerClick(object sender, EventArgs e)
{
string filePath = Server.MapPath("/uploadImg");
const int size = 5242880;
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
if (Request.Files.Count > 0)
{
for (int i = 0; i Request.Files.Count; i++)
{
HttpPostedFile postFile = Request.Files[i];
string uploadFileID = string.Format("#upload{0}", i + 1); //當(dāng)前的上傳控件ID,因?yàn)閖query要調(diào)用就加了#
string msg = null; //提示信息
if (postFile.FileName.Trim().Length = 0)
{
continue;
}
if (postFile.ContentLength > size)
{
msg = "文件太大";
Page.ClientScript.RegisterStartupScript(GetType(), "", "ValidImage(" + uploadFileID + "," + msg + ")", true);//將提示信息發(fā)送到客戶(hù)端
continue;
}
string savePath = Path.Combine(filePath, postFile.FileName); //圖片的保存地址
if (!File.Exists(savePath))
{
postFile.SaveAs(Path.Combine(filePath, postFile.FileName)); //如果文件不存在就保存
}
else
{
msg = "文件" + postFile.FileName + "已經(jīng)存在";
Page.ClientScript.RegisterStartupScript(GetType(), "", "ValidImage(" + uploadFileID + "," + msg + ")", true);//將提示信息發(fā)送到客戶(hù)端
continue;
}
if (IsImg(savePath)) //通過(guò)IsImg方法驗(yàn)證文件是否是圖片,或者格式是否正確
{
SmallImg(postFile.InputStream, postFile.FileName);
}
else
{
msg = "只能上傳JGP、PNG類(lèi)型的圖片,請(qǐng)檢查文件格式是否正確";
Page.ClientScript.RegisterStartupScript(GetType(), "", "ValidImage(" + uploadFileID + "," + msg + ")", true);//將提示信息發(fā)送到客戶(hù)端
File.Delete(savePath); //如果不是圖片就刪除
}
}
}
}
復(fù)制代碼 代碼如下:
#region 驗(yàn)證上傳文件的格式
/// summary>
/// 驗(yàn)證上傳文件是否是圖片
/// /summary>
/// param name="FilePath">文件的保存路徑/param>
/// returns>/returns>
private bool IsImg(string FilePath)
{
using (FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read))
{
bool result = false;
BinaryReader br = new BinaryReader(fs, System.Text.Encoding.UTF8);
string strImg = "";
byte buffer;
try
{
buffer = br.ReadByte();
strImg = buffer.ToString();
buffer = br.ReadByte();
strImg += buffer.ToString();
}
catch
{
fs.Close();
br.Close();
}
if (strImg == "255216" || strImg == "13780")//說(shuō)明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
{
result = true;
}
return result;
}
}
#endregion
復(fù)制代碼 代碼如下:
#region 將圖片生成縮略圖
/// summary>
/// 生成縮略圖
/// /summary>
private void SmallImg(Stream oStream, string FileName)
{
using (System.Drawing.Image img = System.Drawing.Image.FromStream(oStream))
{
int newWidth = 100;
int newHeight = 80;
int oldWidth = img.Width;
int oldHeight = img.Height;
if (oldWidth > oldHeight)
{
newHeight = (int)Math.Floor((double)oldHeight * (double)newWidth / (double)oldWidth);
}
else
{
newWidth = (int)Math.Floor((double)oldWidth * (double)newHeight / (double)oldHeight);
}
using (Bitmap bmp = new Bitmap(newWidth, newHeight))
{
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.Transparent);
g.InterpolationMode = InterpolationMode.High;
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawImage(img, new Rectangle(0, 0, newWidth, newHeight), new Rectangle(0, 0, oldWidth, oldHeight), GraphicsUnit.Pixel);
string newFileName = Path.GetFileNameWithoutExtension(FileName) + "_small" + Path.GetExtension(FileName); //縮略圖名稱(chēng)
string filePath = Server.MapPath("/uploadImg/") + newFileName;
bmp.Save(filePath);
}
}
}
}
#endregion
代碼有很多需要改進(jìn)的地方,希望大家多多指點(diǎn)。
您可能感興趣的文章:- C#(.net)水印圖片的生成完整實(shí)例
- asp.net(C#)使用QRCode生成圖片中心加Logo或圖像的二維碼實(shí)例
- c# .net 生成圖片驗(yàn)證碼的代碼
- C#動(dòng)態(tài)生成PictureBox并指定圖片的方法
- C#生成條形碼圖片的簡(jiǎn)單方法
- c#生成圖片縮略圖的類(lèi)(2種實(shí)現(xiàn)思路)
- C#實(shí)現(xiàn)的pdf生成圖片文字水印類(lèi)實(shí)例
- c#生成自定義圖片方法代碼實(shí)例
- C#實(shí)現(xiàn)在底圖上動(dòng)態(tài)生成文字和圖片