chyel-love'net

本人今年大四,即将毕业,准备从事dotnet程序开发业,
擅长b/s开发,熟悉三层结构,希望有愿意收留本人者,请与我联系。QQ:109267323
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

文件上传 (file field)

Posted on 2005-09-03 18:17  chyel-love  阅读(982)  评论(1编辑  收藏  举报

在网上找了半天做文件上传的,都是VB版的,没有找到C#的,就用C#翻译了一下!
还是用,<INPUT TYPE=FILE RUNAT=SERVER>做的.

  下面是aspx文件代码:

<form id="Form1" method="post" runat="server">
            
<INPUT id="UpFiles" runat="server" type="file">
            
<asp:Button id="upFileBtn" runat="server" Text="UpDate"></asp:Button><BR>
            
<BR>
            
<asp:Label id="Label1" runat="server" Width="288px" Height="144px">Label</asp:Label>
        
</form>

   aspx.cs CODE:

private void upFileBtn_Click(object sender, System.EventArgs e)
        
{
            
if(this.UpFiles.PostedFile.ContentLength==0)
                Label1.Text
="上传文件不成功或者发生错误";
            
else
            
{
                
//获取上传文件名
                string temp=this.UpFiles.PostedFile.FileName;
                
int intT=temp.LastIndexOf("\\");
                temp
=temp.Substring(intT+1);
                
//保存文件
                this.UpFiles.PostedFile.SaveAs(Server.MapPath(".")+"\\Files\\"+temp);
                Label1.Text
="文件上传成功!上传文件名"+temp;

            }

        }