分享
 
 
 

ASP.NET之上传文件管理策略

王朝asp·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

最近做的项目跟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 style="border-right: #ffff00 thin dashed; border-top: #ffff00 thin dashed; margin-bottom: 4px; margin-left: 55px; border-left: #ffff00 thin dashed; line-height: normal; margin-right: 55px; padding-top: 1px; border-bottom: #ffff00 thin dashed; letter-spacing: normal; position: static; background-color: silver; text-align: center">

<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"/>

&nbsp;<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"/><br />

</b>

<br />

<br />

&nbsp;</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 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

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

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有