天涯之外

导航

C#上传文件到网站指定目录

页面代码--

 

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3.   
  4.   
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  6.   
  7. <html xmlns="http://www.w3.org/1999/xhtml">  
  8.   
  9. <head runat="server">  
  10.   
  11.     <title>无标题页</title>  
  12.   
  13. </head>  
  14.   
  15. <body>  
  16.   
  17.     <form id="form1" runat="server" enctype="multipart/form-data">  
  18.   
  19.         <div>  
  20.   
  21.              <input id="fileUp" name="File1" type="file" runat="server" />  
  22.   
  23.             <asp:Button ID="btnUpFile" runat="server" Text="上传" OnClick="btnUpFile_Click" />  
  24.   
  25.             <asp:Label ID="strState" runat="server" />  
  26.   
  27.             <img src="upfile/2.jpg" />  
  28.   
  29.                
  30.   
  31.     </form>  
  32.   
  33. </body>  
  34.   
  35. </html>  

 

cs代码--

 

  1. using System;   
  2.   
  3. using System.Data;   
  4.   
  5. using System.Configuration;   
  6.   
  7. using System.Web;   
  8.   
  9. using System.Web.Security;   
  10.   
  11. using System.Web.UI;   
  12.   
  13. using System.Web.UI.WebControls;   
  14.   
  15. using System.Web.UI.WebControls.WebParts;   
  16.   
  17. using System.Web.UI.HtmlControls;   
  18.   
  19.   
  20.   
  21. public partial class _Default:System.Web.UI.Page   
  22.   
  23. {   
  24.   
  25.     protected void Page_Load(object sender,EventArgs e)   
  26.   
  27.     {   
  28.   
  29.   
  30.   
  31.     }   
  32.   
  33.     protected void btnUpFile_Click(object sender,EventArgs e)   
  34.   
  35.     {   
  36.   
  37.         String filepath = "",fileExtName = "",mFileName,mPath;   
  38.   
  39.   
  40.   
  41.         System.Text.StringBuilder strMsg = new System.Text.StringBuilder("上传信息<hr color=red>");   
  42.   
  43.   
  44.   
  45.         if(fileUp.PostedFile.FileName != "")   
  46.   
  47.         {   
  48.   
  49.             //取得文件路径   
  50.   
  51.             filepath = fileUp.PostedFile.FileName;   
  52.   
  53.             //取得文件扩展名   
  54.   
  55.             fileExtName = filepath.Substring(filepath.LastIndexOf(".") + 1);   
  56.   
  57.   
  58.   
  59.             //取得服务器上的相对路径   
  60.   
  61.             <FONT color=#ff0000>mPath = Server.MapPath("upfile/");   
  62. </FONT>   
  63.             //取得文件名   
  64.   
  65.             <FONT color=#ff0000>mFileName = filepath.Substring(filepath.LastIndexOf("\\") + 1);   
  66. </FONT>   
  67.   
  68.   
  69.             //取得上传文件的信息   
  70.   
  71.             strMsg.Append("上传文件类型:" + fileUp.PostedFile.ContentType.ToString() + "<br>");   
  72.   
  73.             strMsg.Append("客户端文件路径:" + fileUp.PostedFile.FileName + "<br>");   
  74.   
  75.             strMsg.Append("上传文件名:" + mFileName + "<br>");   
  76.   
  77.             strMsg.Append("扩展名:" + fileExtName);   
  78.   
  79.   
  80.   
  81.             //保存文件到指定目录   
  82.   
  83.             fileUp.PostedFile.SaveAs(mPath + mFileName);   
  84.   
  85.             strState.Text = strMsg.ToString();   
  86.   
  87.         }   
  88.   
  89.     }   
  90.   
  91. }  

这里没有给出数据库的写入,其实只要把mPath,mFileName的值写入数据库即可。

 

posted on 2009-03-11 13:06  天涯之外  阅读(1382)  评论(0编辑  收藏  举报