ASP.NET之上传文件管理策略
2008-02-22 09:43:09来源:互联网 阅读 ()
最近做的项目跟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"/>
<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 />
</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
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash