在ASP时代要上传一个文件是很麻烦的事,有了ASP.Net事情就变得简单.这里我给出一个实例,希望能对朋友们有帮助.文件上传的实例,
来自http://www.chinabs.net/">中国BS网 <%@
Import Namespace="System.IO" %> <%@ page
Language="C#" debug="true"
%> <html> <head> <title>上传文件 ,
http://www.chinabs.net/
</title> <script language="C#"
runat="server"> //This method is called when the
"upload" button id pressed public void
UploadFile(object sender , EventArgs
E) { //检查上传文件不为空
if(myFile.PostedFile!=null) {
string nam =
myFile.PostedFile.FileName
; //取得文件名(抱括路径)里最后一个"."的索引 int
i=
nam.LastIndexOf("."); //取得文件扩展名 string
newext
=nam.Substring(i); //这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复 DateTime
now = DateTime.Now; string
newname=now.DayOfYear.ToString()+myFile.PostedFile.ContentLength.ToString(); //保存文件到你所要的目录,这里是IIS根目录下的upload目录.你可以改变. //注意:
我这里用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替 myFile.PostedFile.SaveAs(Server.MapPath("file://upload//)); //得到这个文件的相关属性:文件名,文件类型,文件大小 fname.Text=myFile.PostedFile.FileName; fenc.Text=myFile.PostedFile.ContentType
; fsize.Text=myFile.PostedFile.ContentLength.ToString();
} }
</script> </head> <body> <center> <h3>
文件上传的实例, 来自<a href="http://www.chinabs.net/">中国BS网</a></h3> <form
id="uploderform" method="post" action="FileUpload.aspx"
enctype="multipart/form-data" runat="server"
> <table border="1" cellspacing="2" cellpadding="2"
> <tr>
<td><h5>选择要上传的文件:</h5></td</tr> <tr> <td> <input
type="file" id="myFile" runat="server"
NAME="myFile"> </td> </tr> <tr><td> <input
type="button" value="上 传" OnServerClick="UploadFile"
runat="server" ID="Button1"
NAME="Button1"> </td></tr> </table> </form> <br> <br> <table
border="1"
cellspacing="2"> <tr><td><b>文件资料</b></td> <td> </td> </tr> <tr> <td>文件名
:</td> <td><asp:label id="fname" text=""
runat="server"
/></td></tr> <tr> <td>文件类型
:</td> <td><asp:label id="fenc"
runat="server"
/></td></tr> <tr> <td>文件大小
:(in bytes)</td> <td><asp:label id="fsize"
runat="server"
/></td></tr> </table> <br> <br> <br> </center> </body> </htm |