利用ASP.NET向服务器上传文件[转]
生活随笔
收集整理的這篇文章主要介紹了
利用ASP.NET向服务器上传文件[转]
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文件上傳技術(shù)是一個(gè)很實(shí)用的技術(shù),有著很廣泛的應(yīng)用,在ASP.NET自身的前一個(gè)版本ASP里實(shí)現(xiàn)這個(gè)功能,就必須使用第三方的組件或者自己開(kāi)發(fā)組件了,現(xiàn)在,用ASP.NET實(shí)現(xiàn)起來(lái)就簡(jiǎn)單得多了,我們不需要使用任何組件就可以實(shí)現(xiàn)上傳的功能了。
????
????單一文件上傳
????下面是實(shí)現(xiàn)單一文件上傳的完整代碼:
?1<%@?Import?Namespace="System"?%>
?2<%@?Import?Namespace="System.Web.HttpPostedFile"?%>
?3<%@?Import?Namespace="System.Web.UI.HtmlControls.HtmlInputFile"?%>
?4<script?language="VB"?runat="server">
?5Sub?UpLoad(Src?As?Object,?E?As?EventArgs)
?6?????If?UploadFile.PostedFile.ContentLength=0?then
?7???????ShowUpLoadFile.innerText="上傳失敗或文件不存在!"
?8??Else
?9??????'獲取文件名
10???dim?Temp()?as?String=Split(UploadFile.PostedFile.FileName,"\")
11???dim?FileName?as?String=Temp(Temp.Length-1)
12???'保存文件
13??UploadFile.PostedFile.SaveAs(Server.MapPath(".")?&?"\Files\"?&?FileName)
14???'顯示上傳結(jié)果
15???ShowUpLoadFile.InnerHtml="文件上傳成功!<br>上傳文件名:"?&?FileName
16?????End?If
17End?Sub
18</script>
19<html>
20<body>
21????<form?runat="server"?enctype="multipart/form-data">
22???????<input?type="file"?id="UploadFile"?runat="server"?size="50"><br>
23???????<asp:button?runat="server"?Text="立即上傳"?onClick="Upload"?/>
24????</form>
25?<hr><br>
26?<span?id="ShowUpLoadFile"?runat="server"></span>
27</body>
28</html>
29
????把上面的代碼保存成.aspx文件,然后在該文件所在目錄下創(chuàng)建一個(gè)存放文件的新目錄Files,運(yùn)行,先感受一下效果,然后再繼續(xù)看下面的講解
????使用ASP.NET上傳文件,需要用到.NET框架的兩個(gè)類:HttpPostedFile和HtmlInputFile,這兩個(gè)類所在的命名空間分別是System.Web.HttpPostedFile和System.Web.UI.HtmlControls.HtmlInputFile,所以我們要在文件開(kāi)頭先導(dǎo)入這兩個(gè)命名空間,
????其中的PostedFile表示上傳到服務(wù)器的文件,它包含幾個(gè)常用的屬性:
???????ContentLength:文件大小;
???????FileName?????:上傳文件的詳細(xì)路徑及文件名;
???????ContentType??:上傳文件的文件類型。
????字符分割函數(shù)Split是用來(lái)取得文件名的,因?yàn)橥ㄟ^(guò)PostedFile.FileName獲得的是詳細(xì)的路徑及文件名。
????
???多文件上傳
???所謂的多文件上傳就是同時(shí)上傳多個(gè)文件,這個(gè)跟單一文件上傳大多是相同的,不同的是多文件上傳是把所有文件作為一個(gè)文件集合一起上傳到服務(wù)器的,我們需要的是把這個(gè)文件集合分解成一個(gè)個(gè)單一的文件,剩下的處理方法就跟單一文件上傳一樣了。
???首先你要知道要最多同時(shí)上傳多少個(gè)文件,然后你就在form之間放多少個(gè)如下的HtmlInput控件:
????<input?type="file"?runat="server"?size="50">
???注意:這里的HtmlInput控件控件是不需要設(shè)置ID的
???那怎么在上傳到服務(wù)器的文件集合中取出一個(gè)個(gè)的文件呢?看下面的代碼:
dim?i?as?integer
???For?i=0?to?Request.Files.Count-1
??????‘使用Request.Files()來(lái)逐個(gè)獲取上傳的文件
??????dim?myFile?as?HttpPostedFile=Request.Files(i)
??????'這里的myFile就相當(dāng)于上例中的PostedFile,可以用myFile.FileName獲得文件名,etc
??????'這里的處理代碼就跟單一文件上傳的一樣了
???Next
C#
<%@Page?Language="C#"%>
<HTML>
?<HEAD>
??<TITLE>
???Multiple?Files?Uploading?Using?C#?-?Demo
??</TITLE>
?</HEAD>
<BODY>
<script?language="C#"?runat="Server">
?//Event?handler?for?the?upload?button
?void?UploadFile(object?Sender,EventArgs?E)
?{
??int?IntLoop=0;
??//Iterating?through?the?Request.Files?collection
??for(IntLoop=0;IntLoop<Request.Files.Count;IntLoop++)
??{
???if?(Request.Files[IntLoop]?!=null)?//Checking?for?valid?file
???{
????//?Since?the?FileName?gives?the?entire?path?we?use?Substring?function?to
rip?of?the?filename.
????string?StrFileName
=Request.Files[IntLoop].FileName.Substring(Request.Files[IntLoop].FileName.L
astIndexOf("\\")?+?1)?;
????string?StrFileType?=?Request.Files[IntLoop].ContentType?;
????int?IntFileSize?=Request.Files[IntLoop].ContentLength;
????//Checking?for?the?file?length.?If?length?is?0?then?file?is?not
uploaded.
????if?(IntFileSize?<=0)
?????Response.Write("?<font?color='Red'?size='2'>Uploading?of?file?"?+
StrFileName?+?"?failed.?</font><br>");
????else
????{
?????//Saving?the?file?to?the?web?server
?????Request.Files[IntLoop].SaveAs(Server.MapPath(".\\"?+?StrFileName));
?????Response.Write(?"<font?color='green'?size='2'>Your?file?"?+?StrFileName
+?"?of?type?"?+?StrFileType?+?"?and?size?"?+?IntFileSize.ToString()?+?"?was
uploaded?successfully.</font><br>");
????}
???}
??}
??Response.Write("<br>Click?<a?href='MultipleFileUploadDemo.aspx'>here</a>
to?upload?more?files");
?}
</script>
<%if(!Page.IsPostBack)
{
%>
<H2?align="center">Multiple?files?uploading?in?ASP.Net?using?C#?-?Demo</H2>
<!--?Declaration?of?server?side?form.Note?the?enctype?attribute?of?the?form
has?to?be?set?to?multipart/form-data?-->
<basefont?size="2">
<form?id="FrmFileUploadDemo"?name="FrmFileUploadDemo"??method="post"
enctype="multipart/form-data"?runat="server">
?<TABLE?align="center"?bgcolor="lightyellow"?cellspacing="5">
??<TR>
???<TD>
????<font?size="2">Select?a?file?to?upload</font>?<input?type="file"
id="File1"?name="File1"?runat="server">
???</TD>
??<TR>
??<TR>
???<TD>
????<font?size="2">Select?a?file?to?upload</font>?<input?type="file"
id="File2"?name="File2"?runat="server">
???</TD>
??<TR>
??<TR>
???<TD>
????<font?size="2">Select?a?file?to?upload</font>?<input?type="file"
id="File3"?name="File3"?runat="server">
???</TD>
??<TR>
??<TR>
???<TD?align="center">
????<asp:button?value="Upload"?Text="Upload"?runat="server"?id="CmdUpload"
onClick="UploadFile"?/>
???</TD>
??</TR>
?</TABLE>
</form>
<%}%>
<BR><BR><BR><BR><BR><BR><BR><BR><BR>
</BODY>
</HTML>
轉(zhuǎn)自:http://www.517sou.net/blogview.asp?logID=399
????
????單一文件上傳
????下面是實(shí)現(xiàn)單一文件上傳的完整代碼:
?1<%@?Import?Namespace="System"?%>
?2<%@?Import?Namespace="System.Web.HttpPostedFile"?%>
?3<%@?Import?Namespace="System.Web.UI.HtmlControls.HtmlInputFile"?%>
?4<script?language="VB"?runat="server">
?5Sub?UpLoad(Src?As?Object,?E?As?EventArgs)
?6?????If?UploadFile.PostedFile.ContentLength=0?then
?7???????ShowUpLoadFile.innerText="上傳失敗或文件不存在!"
?8??Else
?9??????'獲取文件名
10???dim?Temp()?as?String=Split(UploadFile.PostedFile.FileName,"\")
11???dim?FileName?as?String=Temp(Temp.Length-1)
12???'保存文件
13??UploadFile.PostedFile.SaveAs(Server.MapPath(".")?&?"\Files\"?&?FileName)
14???'顯示上傳結(jié)果
15???ShowUpLoadFile.InnerHtml="文件上傳成功!<br>上傳文件名:"?&?FileName
16?????End?If
17End?Sub
18</script>
19<html>
20<body>
21????<form?runat="server"?enctype="multipart/form-data">
22???????<input?type="file"?id="UploadFile"?runat="server"?size="50"><br>
23???????<asp:button?runat="server"?Text="立即上傳"?onClick="Upload"?/>
24????</form>
25?<hr><br>
26?<span?id="ShowUpLoadFile"?runat="server"></span>
27</body>
28</html>
29
????把上面的代碼保存成.aspx文件,然后在該文件所在目錄下創(chuàng)建一個(gè)存放文件的新目錄Files,運(yùn)行,先感受一下效果,然后再繼續(xù)看下面的講解
????使用ASP.NET上傳文件,需要用到.NET框架的兩個(gè)類:HttpPostedFile和HtmlInputFile,這兩個(gè)類所在的命名空間分別是System.Web.HttpPostedFile和System.Web.UI.HtmlControls.HtmlInputFile,所以我們要在文件開(kāi)頭先導(dǎo)入這兩個(gè)命名空間,
????其中的PostedFile表示上傳到服務(wù)器的文件,它包含幾個(gè)常用的屬性:
???????ContentLength:文件大小;
???????FileName?????:上傳文件的詳細(xì)路徑及文件名;
???????ContentType??:上傳文件的文件類型。
????字符分割函數(shù)Split是用來(lái)取得文件名的,因?yàn)橥ㄟ^(guò)PostedFile.FileName獲得的是詳細(xì)的路徑及文件名。
????
???多文件上傳
???所謂的多文件上傳就是同時(shí)上傳多個(gè)文件,這個(gè)跟單一文件上傳大多是相同的,不同的是多文件上傳是把所有文件作為一個(gè)文件集合一起上傳到服務(wù)器的,我們需要的是把這個(gè)文件集合分解成一個(gè)個(gè)單一的文件,剩下的處理方法就跟單一文件上傳一樣了。
???首先你要知道要最多同時(shí)上傳多少個(gè)文件,然后你就在form之間放多少個(gè)如下的HtmlInput控件:
????<input?type="file"?runat="server"?size="50">
???注意:這里的HtmlInput控件控件是不需要設(shè)置ID的
???那怎么在上傳到服務(wù)器的文件集合中取出一個(gè)個(gè)的文件呢?看下面的代碼:
dim?i?as?integer
???For?i=0?to?Request.Files.Count-1
??????‘使用Request.Files()來(lái)逐個(gè)獲取上傳的文件
??????dim?myFile?as?HttpPostedFile=Request.Files(i)
??????'這里的myFile就相當(dāng)于上例中的PostedFile,可以用myFile.FileName獲得文件名,etc
??????'這里的處理代碼就跟單一文件上傳的一樣了
???Next
C#
<%@Page?Language="C#"%>
<HTML>
?<HEAD>
??<TITLE>
???Multiple?Files?Uploading?Using?C#?-?Demo
??</TITLE>
?</HEAD>
<BODY>
<script?language="C#"?runat="Server">
?//Event?handler?for?the?upload?button
?void?UploadFile(object?Sender,EventArgs?E)
?{
??int?IntLoop=0;
??//Iterating?through?the?Request.Files?collection
??for(IntLoop=0;IntLoop<Request.Files.Count;IntLoop++)
??{
???if?(Request.Files[IntLoop]?!=null)?//Checking?for?valid?file
???{
????//?Since?the?FileName?gives?the?entire?path?we?use?Substring?function?to
rip?of?the?filename.
????string?StrFileName
=Request.Files[IntLoop].FileName.Substring(Request.Files[IntLoop].FileName.L
astIndexOf("\\")?+?1)?;
????string?StrFileType?=?Request.Files[IntLoop].ContentType?;
????int?IntFileSize?=Request.Files[IntLoop].ContentLength;
????//Checking?for?the?file?length.?If?length?is?0?then?file?is?not
uploaded.
????if?(IntFileSize?<=0)
?????Response.Write("?<font?color='Red'?size='2'>Uploading?of?file?"?+
StrFileName?+?"?failed.?</font><br>");
????else
????{
?????//Saving?the?file?to?the?web?server
?????Request.Files[IntLoop].SaveAs(Server.MapPath(".\\"?+?StrFileName));
?????Response.Write(?"<font?color='green'?size='2'>Your?file?"?+?StrFileName
+?"?of?type?"?+?StrFileType?+?"?and?size?"?+?IntFileSize.ToString()?+?"?was
uploaded?successfully.</font><br>");
????}
???}
??}
??Response.Write("<br>Click?<a?href='MultipleFileUploadDemo.aspx'>here</a>
to?upload?more?files");
?}
</script>
<%if(!Page.IsPostBack)
{
%>
<H2?align="center">Multiple?files?uploading?in?ASP.Net?using?C#?-?Demo</H2>
<!--?Declaration?of?server?side?form.Note?the?enctype?attribute?of?the?form
has?to?be?set?to?multipart/form-data?-->
<basefont?size="2">
<form?id="FrmFileUploadDemo"?name="FrmFileUploadDemo"??method="post"
enctype="multipart/form-data"?runat="server">
?<TABLE?align="center"?bgcolor="lightyellow"?cellspacing="5">
??<TR>
???<TD>
????<font?size="2">Select?a?file?to?upload</font>?<input?type="file"
id="File1"?name="File1"?runat="server">
???</TD>
??<TR>
??<TR>
???<TD>
????<font?size="2">Select?a?file?to?upload</font>?<input?type="file"
id="File2"?name="File2"?runat="server">
???</TD>
??<TR>
??<TR>
???<TD>
????<font?size="2">Select?a?file?to?upload</font>?<input?type="file"
id="File3"?name="File3"?runat="server">
???</TD>
??<TR>
??<TR>
???<TD?align="center">
????<asp:button?value="Upload"?Text="Upload"?runat="server"?id="CmdUpload"
onClick="UploadFile"?/>
???</TD>
??</TR>
?</TABLE>
</form>
<%}%>
<BR><BR><BR><BR><BR><BR><BR><BR><BR>
</BODY>
</HTML>
轉(zhuǎn)自:http://www.517sou.net/blogview.asp?logID=399
轉(zhuǎn)載于:https://www.cnblogs.com/airfey/archive/2007/01/10/616964.html
總結(jié)
以上是生活随笔為你收集整理的利用ASP.NET向服务器上传文件[转]的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [转][HTML]css属性
- 下一篇: [你必须知道的.NET] 第一回:恩怨情