文件上传的实例, 来自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("\\upload\\"+newname+newext)); 
  //得到这个文件的相关属性:文件名,文件类型,文件大小
  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 /></td></tr>
</table>
<br>
<br>
<br>
</center>
</body>
</htm>

相关文章: