asp.net上传文件的源代码大全
asp.net上传文件的源代码大全
<%@Pagelanguage="c#"src="UPLOAD_CS.aspx.cs"AutoEventWireup="false"Inherits="HowTos.MultipleImageUpdate.UPLOAD"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
<HTML>
<HEAD>
<title>:::i7land无组件多文件上传程序:::</title>
</HEAD>
<body>
<center>
<formid="UPLOAD"method="post"runat="server"enctype="multipart/form-data">
<h3>多文件上传<hr><br>为保证服务器和网站的安全,禁止上传任何.COM或.DLL文件,但允许你上传任何gif、jpg、swf、asp、aspx、htm、html、js、css等网络文件</h3>
<P><INPUTtype="file"runat="server"size="50"></P>
<P><INPUTtype="file"runat="server"size="50"></P>
<P><INPUTtype="file"runat="server"size="50"></P>
<P><INPUTtype="file"runat="server"size="50"></P>
<P><INPUTtype="file"runat="server"size="50"></P>
<P><INPUTtype="file"runat="server"size="50"></P>
<P><INPUTtype="file"runat="server"size="50"></P>
<P><INPUTtype="file"runat="server"size="50"></P>
<P><INPUTtype="file"runat="server"size="50"></P>
<P><INPUTtype="file"runat="server"size="50"></P>
<P><STRONG>::</STRONG>
<asp:LinkButtonid="LinkButton1"runat="server"Font-Names="Verdana"Font-Bold="True"Font-Size="XX-Small">上传</asp:LinkButton><STRONG>::
</STRONG><Ahref="javascript:document.forms[0].reset()"id="LinkButton2"style="FONT-WEIGHT:bold;FONT-SIZE:xx-small;FONT-FAMILY:verdana">
重置</A><STRONG>::</STRONG></P>
<P>
<asp:Labelid="Label1"runat="server"Font-Names="verdana"Font-Bold="True"Font-Size="XX-Small"Width="400px"BorderStyle="None"BorderColor="White"></asp:Label></P>
<P></P>
</form>
</center>
</body>
</HTML>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
<HTML>
<HEAD>
<title>:::i7land无组件多文件上传程序:::</title>
</HEAD>
<body>
<center>
<formid="UPLOAD"method="post"runat="server"enctype="multipart/form-data">
<h3>多文件上传<hr><br>为保证服务器和网站的安全,禁止上传任何.COM或.DLL文件,但允许你上传任何gif、jpg、swf、asp、aspx、htm、html、js、css等网络文件</h3>
<P><INPUTtype="file"runat="server"size="50"></P>
<P><INPUTtype="file"runat="server"size="50"></P>
<P><INPUTtype="file"runat="server"size="50"></P>
<P><INPUTtype="file"runat="server"size="50"></P>
<P><INPUTtype="file"runat="server"size="50"></P>
<P><INPUTtype="file"runat="server"size="50"></P>
<P><INPUTtype="file"runat="server"size="50"></P>
<P><INPUTtype="file"runat="server"size="50"></P>
<P><INPUTtype="file"runat="server"size="50"></P>
<P><INPUTtype="file"runat="server"size="50"></P>
<P><STRONG>::</STRONG>
<asp:LinkButtonid="LinkButton1"runat="server"Font-Names="Verdana"Font-Bold="True"Font-Size="XX-Small">上传</asp:LinkButton><STRONG>::
</STRONG><Ahref="javascript:document.forms[0].reset()"id="LinkButton2"style="FONT-WEIGHT:bold;FONT-SIZE:xx-small;FONT-FAMILY:verdana">
重置</A><STRONG>::</STRONG></P>
<P>
<asp:Labelid="Label1"runat="server"Font-Names="verdana"Font-Bold="True"Font-Size="XX-Small"Width="400px"BorderStyle="None"BorderColor="White"></asp:Label></P>
<P></P>
</form>
</center>
</body>
</HTML>
程序代码:
这是(UPLOAD_CS.aspx.cs):
namespaceHowTos.MultipleImageUpdate
{
publicclassUPLOAD:System.Web.UI.Page
{
protectedSystem.Web.UI.WebControls.LinkButtonLinkButton1;
#regionUserDefinedCode
protectedSystem.Web.UI.WebControls.LabelLabel1;
privatevoidPage_Load(System.Objectsender,System.EventArgse)
{
if(this.IsPostBack)
this.SaveImages();
}
privateSystem.BooleanSaveImages(){
//loopthroughthefilesuploaded
System.Web.HttpFileCollection_files=System.Web.HttpContext.Current.Request.Files;
//Messagetotheuser
System.Text.StringBuilder_message=newSystem.Text.StringBuilder("文件已成功上传:其中.gif.swf文件上传到gifs目录中,网页文件上传到web目录中,.jpg文件上传到jpgs目录中<br>";
try
{
for(System.Int32_iFile=0;_iFile<_files.Count;_iFile++)
{
//Checktomakesuretheuploadedfileisajpgorgif
System.Web.HttpPostedFile_postedFile=_files[_iFile];
System.String_fileName,_fileExtension;
_fileName=System.IO.Path.GetFileName(
_postedFile.FileName);
_fileExtension=System.IO.Path.GetExtension(
_fileName);
if(_fileExtension==".bmp"||_fileExtension==".BMP"||_fileExtension==".gif"||_fileExtension==".GIF"||_fileExtension==".swf"||_fileExtension==".SWF")
{
//SaveFiletotheproperdirectory
_postedFile.SaveAs(
System.Web.HttpContext.Current.Request.MapPath(
"../uploadfiles/images"+_fileName);
_message.Append(_fileName+"<BR>";
}
elseif(_fileExtension==".jpg"||_fileExtension==".JPG"
{
//SaveFiletotheproperdirectory
_postedFile.SaveAs(
System.Web.HttpContext.Current.Request.MapPath(
"../uploadfiles/images"+_fileName);
_message.Append(_fileName+"<BR>";
}
elseif(_fileExtension==".asp"||_fileExtension==".ASP"||_fileExtension==".aspx"||_fileExtension==".ASPX"||_fileExtension==".htm"||_fileExtension==".HTM"||_fileExtension==".HTML"||_fileExtension==".html"||_fileExtension==".cs"||_fileExtension==".CS"||_fileExtension==".txt"||_fileExtension==".TXT"||_fileExtension==".RAR"||_fileExtension==".rar"||_fileExtension==".zip"||_fileExtension==".ZIP"||_fileExtension==".INC"||_fileExtension==".inc"||_fileExtension==".xml"||_fileExtension==".XML"||_fileExtension==".MDB"||_fileExtension==".mdb"||_fileExtension==".JS"||_fileExtension==".js"||_fileExtension==".vb"||_fileExtension==".VB"||_fileExtension==".css"||_fileExtension==".CSS"||_fileExtension==".CONFIG"||_fileExtension==".config"||_fileExtension==".exe"||_fileExtension==".EXE"
{
//SaveFiletotheproperdirectory
_postedFile.SaveAs(
System.Web.HttpContext.Current.Request.MapPath(
"../uploadfiles"+_fileName);
_message.Append(_fileName+"<BR>";
}
elseif(_fileExtension==".COM"||_fileExtension==".com"||_fileExtension==".dll"||_fileExtension==".DLL"
continue;
else{
_message.Append(_fileName+"<fontcolor="red">上传失败,可能是文件格式不对!本系统为保证网站和服务器的安全,禁止上传任何.COM或.DLL文件</font><BR>";
}
}
Label1.Text=_message.ToString();
returntrue;
}
catch(System.ExceptionEx)
{
Label1.Text=Ex.Message;
returnfalse;
}
}
#endregion
#regionWebFormDesignergeneratedcode
overrideprotectedvoidOnInit(System.EventArgse)
{
//
//CODEGEN:ThiscallisrequiredbytheASP.NETWebFormDesigner.
//
InitializeComponent();
base.OnInit(e);
}
///<summary>
///RequiredmethodforDesignersupport-donotmodify
///thecontentsofthismethodwiththecodeeditor.
///</summary>
privatevoidInitializeComponent()
{
this.Load+=newSystem.EventHandler(this.Page_Load);
}
#endregion
}
}
namespaceHowTos.MultipleImageUpdate
{
publicclassUPLOAD:System.Web.UI.Page
{
protectedSystem.Web.UI.WebControls.LinkButtonLinkButton1;
#regionUserDefinedCode
protectedSystem.Web.UI.WebControls.LabelLabel1;
privatevoidPage_Load(System.Objectsender,System.EventArgse)
{
if(this.IsPostBack)
this.SaveImages();
}
privateSystem.BooleanSaveImages(){
//loopthroughthefilesuploaded
System.Web.HttpFileCollection_files=System.Web.HttpContext.Current.Request.Files;
//Messagetotheuser
System.Text.StringBuilder_message=newSystem.Text.StringBuilder("文件已成功上传:其中.gif.swf文件上传到gifs目录中,网页文件上传到web目录中,.jpg文件上传到jpgs目录中<br>";
try
{
for(System.Int32_iFile=0;_iFile<_files.Count;_iFile++)
{
//Checktomakesuretheuploadedfileisajpgorgif
System.Web.HttpPostedFile_postedFile=_files[_iFile];
System.String_fileName,_fileExtension;
_fileName=System.IO.Path.GetFileName(
_postedFile.FileName);
_fileExtension=System.IO.Path.GetExtension(
_fileName);
if(_fileExtension==".bmp"||_fileExtension==".BMP"||_fileExtension==".gif"||_fileExtension==".GIF"||_fileExtension==".swf"||_fileExtension==".SWF")
{
//SaveFiletotheproperdirectory
_postedFile.SaveAs(
System.Web.HttpContext.Current.Request.MapPath(
"../uploadfiles/images"+_fileName);
_message.Append(_fileName+"<BR>";
}
elseif(_fileExtension==".jpg"||_fileExtension==".JPG"
{
//SaveFiletotheproperdirectory
_postedFile.SaveAs(
System.Web.HttpContext.Current.Request.MapPath(
"../uploadfiles/images"+_fileName);
_message.Append(_fileName+"<BR>";
}
elseif(_fileExtension==".asp"||_fileExtension==".ASP"||_fileExtension==".aspx"||_fileExtension==".ASPX"||_fileExtension==".htm"||_fileExtension==".HTM"||_fileExtension==".HTML"||_fileExtension==".html"||_fileExtension==".cs"||_fileExtension==".CS"||_fileExtension==".txt"||_fileExtension==".TXT"||_fileExtension==".RAR"||_fileExtension==".rar"||_fileExtension==".zip"||_fileExtension==".ZIP"||_fileExtension==".INC"||_fileExtension==".inc"||_fileExtension==".xml"||_fileExtension==".XML"||_fileExtension==".MDB"||_fileExtension==".mdb"||_fileExtension==".JS"||_fileExtension==".js"||_fileExtension==".vb"||_fileExtension==".VB"||_fileExtension==".css"||_fileExtension==".CSS"||_fileExtension==".CONFIG"||_fileExtension==".config"||_fileExtension==".exe"||_fileExtension==".EXE"
{
//SaveFiletotheproperdirectory
_postedFile.SaveAs(
System.Web.HttpContext.Current.Request.MapPath(
"../uploadfiles"+_fileName);
_message.Append(_fileName+"<BR>";
}
elseif(_fileExtension==".COM"||_fileExtension==".com"||_fileExtension==".dll"||_fileExtension==".DLL"
continue;
else{
_message.Append(_fileName+"<fontcolor="red">上传失败,可能是文件格式不对!本系统为保证网站和服务器的安全,禁止上传任何.COM或.DLL文件</font><BR>";
}
}
Label1.Text=_message.ToString();
returntrue;
}
catch(System.ExceptionEx)
{
Label1.Text=Ex.Message;
returnfalse;
}
}
#endregion
#regionWebFormDesignergeneratedcode
overrideprotectedvoidOnInit(System.EventArgse)
{
//
//CODEGEN:ThiscallisrequiredbytheASP.NETWebFormDesigner.
//
InitializeComponent();
base.OnInit(e);
}
///<summary>
///RequiredmethodforDesignersupport-donotmodify
///thecontentsofthismethodwiththecodeeditor.
///</summary>
privatevoidInitializeComponent()
{
this.Load+=newSystem.EventHandler(this.Page_Load);
}
#endregion
}
}