using System.Web.Security;
//上传文件
private void Button2_ServerClick(object sender, System.EventArgs e)
{
HttpPostedFile hpf = htmlfile.PostedFile;

char[] de = {'\\'};
string[] file1 = hpf.FileName.Split( de );
string file2 = file1[ file1.Length - 1 ];hpf.SaveAs( Server.MapPath("/mail") + "\\" + file2 );
}
<INPUT id="htmlfile" type="file" name="htmlfile" runat="server"><INPUT id="Button2" title="上传文件" type="button" value="上传文件" name="Button2" runat="server">

解决方案 »

  1.   

    aspx文件
    <INPUT id="File1" type="file" size="50" name="File1" runat="server">
    <asp:button id="btn_Upfile" runat="server" Text="上传"></asp:button>
    aspx.vb文件
    Private Function UpFile() As Boolean
            Dim filena As String = Path.GetFileName(File1.PostedFile.FileName)
            Dim filesm As String = Me.tbx_Upfile.Text.Trim
            If Len(filena) > 0 And Len(filesm) > 0 Then
                Dim filepath As String = Server.MapPath("../fileupLoad/" + filena)
                UpfilePath = "../fileupLoad/" + Path.GetFileName(File1.PostedFile.FileName)
                If (File.Exists(filepath)) Then
                    lbl_error.Text = "上传文件重名,请改名后再上传!"
                    Return False
                Else
                    If Not (File1.PostedFile Is Nothing) Then
                        Try
                            File1.PostedFile.SaveAs(filepath)
                            lbl_error.Text = "<b>成功上传!</b>"
                            Return True
                        Catch ex As Exception
                            lbl_error.Text = "保存文件时出错<b>"
                            Return False
                        End Try                End If
                End If
            Else
                Me.lbl_error.Text = "请先输入附件说明或者选择要上传的附件!"
                Exit Function
            End If    End Function
      

  2.   

    private void Button1_Click(object sender, System.EventArgs e)
    {
    if(this.myFile.PostedFile!=null) //upload textbox
    {
    //接收路径
    string strPath=Request.QueryString["path"]; //current direction //如果接收的路径为空,则获取当前路径
    if(strPath==null)
    {
    strPath=Request.ServerVariables["APPL_PHYSICAL_PATH"];
    }
    else
    {
    if(strPath.Substring(strPath.Length-1,1)!="\\")
    {
    strPath=strPath+"\\";
    }
    } //处理字符串
    string strName=this.myFile.PostedFile.FileName;
    int i=strName.LastIndexOf("\\");
    string strNewName=strName.Substring(i);
    string strFullName=strPath+strNewName;
    //保存文件
    this.myFile.PostedFile.SaveAs(strFullName);
    //Response.Redirect("index.aspx?path="+strPath);
    this.Label2.Text="<center><font color=red>上传成功</font></center>";
    ListFile();
    }
    }
      

  3.   

    samilly(风沙狼) 的方法好块哦,不过没有ListFile()这个函数,该则怎么写呢