最近做的項目跟ASP.Net上傳文件內容有關,故將代碼貼出,以便網友查閱,提供解決此類問題思路:如出現任何不理解問題,請留言,及時幫您解決!
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.ASPx.cs" Inherits="Login" %>
<!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>::::上傳管理系統::::</title>
</head>
<body>
<form id="form1" runat="server">
<div >
<br />
<ASP:Label ID="LabelTitle" runat="server" Font-Bold="True" Font-Size="XX-Large" ForeColor="Black"
Text="上傳管理"></ASP:Label><br />
<br />
<ASP:FileUpload ID="FileUpload1" runat="server" Width="369px"/>
<ASP:Button ID="ButtonUp" runat="server" Height="21px" Text="上 傳" Width="67px" OnClick="ButtonUp_Click" /><br />
<br />
<asp:Label ID="LabelTitle2" runat="server" ForeColor="Red" Text="*上傳文件不能超過2M"></ASP:Label><br />
<b>原文件名:</b><span id ="FileName" runat="server" /><br />
<b>上傳到服務器:</b><span id="SaveDir" runat="server" /><br />
<b>縮略圖地址:</b><span id ="sSaveDir" runat ="server" /><br />
<b>文件類型:</b><span id ="FileType" runat ="server" /><br />
<b>文件大小:</b><span id="FileLength" runat ="server" /><br />
<b>文件拓展名:</b><span id="FileExtention" runat="server" /><br />
<b>上傳日期:<br /><span id ="UpDateTime" runat ="server" /><br />
圖片預覽:<br />
<asp:Image ID="Image1" runat="server" ImageUrl="~/Login.ASPx" BorderColor="#0000C0"/>&l
*******************************************************************************
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 MWO.Model.Info;
using MWO.DAL.Info;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ButtonUp_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.FileName != null)
{
try
{
FileName.InnerHtml = FileUpload1.PostedFile.FileName;//GetPhotoName
UpDateTime.InnerHtml = DateTime.Now.ToShortDateString();//UpDateTime
#region SetPhotoSize
FileLength.InnerHtml = CountSize(FileUpload1.PostedFile.ContentLength);
if (FileUpload1.PostedFile.ContentLength > 1024 * 1024 * 2)
{
Response.Write("<script>alert('圖片不能超過規定大小!');</script>");
}
else
{
#region SetPhotoFormat
FileType.InnerHtml = FileUpload1.PostedFile.ContentType;
FileExtention.InnerHtml = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName).ToUpper();
string m_FileName = "www.MWO.Com-" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff").Replace(".", "-") + FileExtention.InnerHtml;
string m_sFileName = "www.MWO.Com-" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff").Replace(".", "-") + FileExtention.InnerHtml;
if (FileExtention.InnerHtml == ".JPG")
{
string m_SavePath = Server.MapPath("./photo/") + m_FileName;
SaveDir.InnerHtml = m_SavePath;
#region SetSmallPhoto
this.MakeSmallImg(FileUpload1.PostedFile, this.Server.MapPath("./SPhoto/") + m_sFileName, 118, 90);
string m_sSavePath = this.Server.MapPath("./SPhoto/") + m_sFileName;
sSaveDir.InnerHtml = m_sSavePath;
#endregion
FileUpload1.PostedFile.SaveAs(m_SavePath);
Response.Write("<script>alert('圖片文件保存成功!');</script>");
#region SaveDataBasee
FileType.InnerHtml = FileUpload1.PostedFile.ContentType;
FileExtention.InnerHtml = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName).ToUpper();
string m_FileName = "www.MWO.Com-" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff").Replace(".", "-") + FileExtention.InnerHtml;
string m_sFileName = "www.MWO.Com-" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff").Replace(".", "-") + FileExtention.InnerHtml;
if (FileExtention.InnerHtml == ".JPG")
{
string m_SavePath = Server.MapPath("./photo/") + m_FileName;
SaveDir.InnerHtml = m_SavePath;
#region SetSmallPhoto
this.MakeSmallImg(FileUpload1.PostedFile, this.Server.MapPath("./SPhoto/") + m_sFileName, 118, 90);
string m_sSavePath = this.Server.MapPath("./SPhoto/") + m_sFileName;
sSaveDir.InnerHtml = m_sSavePath;
#endregion
FileUpload1.PostedFile.SaveAs(m_SavePath);
Response.Write("<script>alert('圖片文件保存成功!');</script>");
#region SaveDataBasee
PhotoInfo m_PInfo = new PhotoInfo();
m_PInfo.Organization = "5173";
m_PInfo.PicName = FileName.InnerHtml;
m_PInfo.ServerName = "GLSDB";
m_PInfo.Cdate = DateTime.Now;
m_PInfo.Exp = FileExtention.InnerHtml;
m_PInfo.Flag = 0;
m_PInfo.GameNickName = "Bruce";//TextBox控件內容
m_PInfo.MoonStar = 0;
m_PInfo.ShowIndex = 0;
m_PInfo.SPicHttpAddr = m_sSavePath;
m_PInfo.Type = 0;
m_PInfo.Vote = 0;
m_PInfo.PicHttpAddr=m_SavePath;
PhotoDAO m_PDao = new PhotoDAO();
m_PDao.Insert(m_PInfo);
#endregion
}
else
{
Response.Write("<script>alert('圖片格式不正確,請選擇圖片文件!');</script>");
}
#endregion
}
#endregion
}
catch (Exception m_Ex)
{
Response .Write ("<script>alert('"+m_Ex.ToString ()+"');</script>");
}
}
else if (FileUpload1.PostedFile.FileName == "")
{
Response.Write("<script>alert('上傳文件不能為空!');</script>");
}
}
#region
/// <summary>
/// 計算文件大小函數,Size為字節大小
/// </summary>
/// <param name="Size">初始文件大小</param>
/// <returns></returns>
public string CountSize(long Size)
{
string m_strSize = "";
long FactSize = 0; FactSize = Size;
if (FactSize <= 1024)
m_strSize = FactSize.ToString() + "Byte";
if(FactSize>=1024&&FactSize<=1048576)
m_strSize=(FactSize/1024).ToString ()+"K";
if (FactSize >= 1048576 && FactSize <= 10485760)
m_strSize = (FactSize / 1024 / 1024).ToString() + "M";
return m_strSize;
}
#endregion
#region SaveSmallPhoto
/// <summary>
/// 高清晰縮略圖算法
/// </summary>
/// <param name="postFile">圖片文件對象</param>
/// <param name="saveImg">要保存為縮略圖的源文件</param>
/// <param name="Width">寬度</param>
/// <param name="Height">高度</param>
public void MakeSmallImg(System.Web.HttpPostedFile postFile, string saveImg, System.Double Width, System.Double Height)
{
//SourcePhotoName
string m_OriginalFilename = postFile.FileName;
string m_strGoodFile = saveImg;
//GetPhotoObject From SourceFile
System.Drawing.Image m_Image = System.Drawing.Image.FromStream(postFile.InputStream, true);
System.Double NewWidth, NewHeight;
if (m_Image.Width > m_Image.Height)
{
NewWidth = Width;
NewHeight = m_Image.Height * (NewWidth / m_Image.Width);
}
else
{
NewHeight = Height;
NewWidth = (NewHeight / m_Image.Height) * m_Image.Width;
}
if (NewWidth > Width)
{
NewWidth = Width;
}
if (NewHeight > Height)
{
NewHeight = Height;
}
//GetPhotoSize
System.Drawing.Size size =new System.Drawing.Size((int)NewWidth, (int)NewHeight);
//The New of Bimp Photo
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);
// The New of Palette
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
// Set HightQuality Arithmetic For Graphics
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//設置高質量,低速度呈現平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//ClearCanvas
g.Clear(System.Drawing.Color.White);
//在指定位置畫圖
g.DrawImage(m_Image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, m_Image.Width, m_Image.Height),
System.Drawing.GraphicsUnit.Pixel);
//SavePhoto Of HightFocus
bitmap.Save(m_strGoodFile, System.Drawing.Imaging.ImageFormat.Jpeg);
//DisposeRes
g.Dispose();
m_Image.Dispose();
bitmap.Dispose();
}
#endregion
}