打开aspx文件,在你的<form>标签中加上enctype="multipart/form-data"像这样: <form id="Form1" runat="Server" enctype="multipart/form-data">

解决方案 »

  1.   

    哈哈,老兄看来你我遇到同样的问题了
    我的不仅仅为空,而且还报错。
    呵呵,咱两看来均为asp.net菜鸟
      

  2.   

    tim大哥,我加了哪个东东啊,怎么还是无法取得postedfile.filename?
      

  3.   

    to:楼上的楼上的,我每个页面都有“enctype="multipart/form-data”to:楼上的:“呵呵”
      

  4.   

    Sorry,是把你们的代码贴出来看看:)
      

  5.   

    我在asp.net中用file 组件上传文件,我点击提交按钮时的按钮事件如下:
    Dim lstfilename As String
    lstfilename = picFile.PostedFile.FileName就这么简单的代码都出错.HTML显示如下:
    <form id="Form1" method="post" encType=" multipart/form-data " runat="server">
    <INPUT id="picFile" name="picfile" style="Z-INDEX: 123; LEFT: 408px; WIDTH: 259px; POSITION: absolute; TOP: 95px; HEIGHT: 22px" type="file" size="24" runat="server" width="30">
    </form>报错为:
    server error请教各位大哥,这是为什么啊?拜托了
      

  6.   

    webform1.aspx:<form id="Form1" method="post" encType="multipart/form-data" runat="server">
    <FONT face="宋体"><INPUT id="myfile" style="Z-INDEX: 101; LEFT: 130px; POSITION: absolute; TOP: 150px" type="file" runat="server">
    <asp:textbox id="TextBox1" style="Z-INDEX: 104; LEFT: 131px; POSITION: absolute; TOP: 179px" runat="server"></asp:textbox>&nbsp;
    <asp:button id="Button1" style="Z-INDEX: 103; LEFT: 366px; POSITION: absolute; TOP: 147px" runat="server" Text="up" Width="64px"></asp:button>
    <DIV id="myDiv" style="Z-INDEX: 102; LEFT: 128px; WIDTH: 289px; POSITION: absolute; TOP: 54px; HEIGHT: 92px" runat="server" ms_positioning="FlowLayout">
    <P><asp:label id="Label2" runat="server" Width="191px">Label</asp:label></P>
    <P><asp:label id="Label1" runat="server" Width="181px">Label</asp:label></P>
    <P>
    <asp:Label id="Label3" runat="server" Width="169px">Label</asp:Label></P>
    </DIV>
    </FONT>
    </form>
    webform1.aspx.cs:private void Button1_Click(object sender, System.EventArgs e) {
    if (myfile.PostedFile == null || myfile.PostedFile.ContentLength == 0){
    this.Label1.Text = "null";
    return;
    }
    else{
    Label1.Text = myfile.PostedFile.FileName;
    Label2.Text = myfile.PostedFile.ContentLength.ToString();
    Label3.Text = myfile.PostedFile.ContentType; string[] fileSplit = myfile.PostedFile.FileName.Split('\\');
    string fileName = fileSplit[fileSplit.Length -1];
    myfile.PostedFile.SaveAs(Server.MapPath(fileName));
    this.Label1.Text += " " + this.TextBox1.Text;
    }
    }
      

  7.   

    To zhp97(海天一线) 
    encType=" multipart/form-data "改为:encType="multipart/form-data"中间不能有空格
      

  8.   

    To cwbboy(好想失恋)
    给你一个简单的例子,已经测试成功,你先把代码粘回去看看可以不可以,可以的话,再对照看看你的代码是什么问题这是aspx:<%@ Page language="c#" Codebehind="FileUploadSimple.aspx.cs" AutoEventWireup="false" Inherits="CsdnTest.FileUploadSimple" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>FileUploadSimple</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body>
    <form id="FileUploadSimple" method="post" runat="server" enctype="multipart/form-data">
    <input type="file" runat="server" id="upload">
    <br>
    <asp:Button ID="btnUpload" Runat="server" Text="Upload"></asp:Button>
    </form>
    </body>
    </HTML>
      

  9.   

    这是 .cs:using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace CsdnTest
    {
    /// <summary>
    /// Summary description for FileUploadSimple.
    /// </summary>
    public class FileUploadSimple : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button btnUpload;
    protected System.Web.UI.HtmlControls.HtmlInputFile upload;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {    
    this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void btnUpload_Click(object sender, System.EventArgs e)
    {
    upload.PostedFile.SaveAs(Server.MapPath(".")+"\\theUploadedFile.txt");
    Response.Write("Upload successful.");
    }
    }
    }
      

  10.   

    啊,妈妈的,被这小小的玩意给害死了,我下午弄到现在啊!!!
    谢谢tim哥。来个kiss。:)
      

  11.   

    楼上的,如果我向你这么写,肯定报错
    private void btnUpload_Click(object sender, System.EventArgs e)
    {
    upload.PostedFile.SaveAs(Server.MapPath(".")+"\\theUploadedFile.txt");
    Response.Write("Upload successful.");
    }我的PostedFile是为空。。
      

  12.   

    to:zhp97(海天一线) 问题解决了?。。恭喜啊,我的问题是在使用用户控间时产生的,单独的文件上传是可以的,但作为用户控间使用时,就出问题,而且,在一个地方可以,其他地方都不行,(虽然其他地方的代码跟他一样)
      

  13.   

    timmy3310(Tim) :  如果不选文件,你的代码会出错。。
      

  14.   

    知我者,zhp97(海天一线)也。  没想到我如此高深的用意,竟然也有人能悟出来
      

  15.   

    to: zhp97(海天一线)    知已啊,就凭你我知已一场,20分归你了:)
      

  16.   

    To cwbboy(好想失恋)不选当然有问题,要不然也不是简单的例子了,只是给你看看基本的用法,所以没做过多的判断从你的描述看来,你的问题不在这里,应该是别的地方没有控制好,没有代码我也不敢判断问题出在哪里
      

  17.   

    不行,给tim吧,他在认真回答问题。
    不过还是谢谢你:)
    呵呵,我也与你一样,下次我取个名字“自恋成狂”
    (因为爱别人通常没结果,还是爱自己吧:))
      

  18.   

    我在网吧啊,网吧哪有.net 啊。。代码就更没有了,明天发吧,不过,我的代码是vb.net的
      

  19.   

    谢谢timmy3310(Tim)啊,明天发代码给你。。麻烦你了。。:)
      

  20.   

    以下是保存附件的用户控件代码 
    '保存附件
        Public Function SaveDoc(ByVal Sourceid As Integer, ByVal TypeId As Integer)
            Try            Dim documents As New ASPNetPortal.CommonDocument()
                Dim Fileobj As System.Web.UI.HtmlControls.HtmlInputFile
                Fileobj = CType(FindControl("FileUpload"), System.Web.UI.HtmlControls.HtmlInputFile)
                If Not (FileUpload.PostedFile Is Nothing) Then
                    If Not FileUpload.PostedFile.FileName.Trim().Length = 0 Then
                        Dim phyiscalPath As String
                        Dim virtualPath As String = "~/uploads/" & Path.GetFileName(FileUpload.PostedFile.FileName)
                        phyiscalPath = Server.MapPath(virtualPath)
                        FileUpload.PostedFile.SaveAs(phyiscalPath)
                        documents.UpdateDocument(Sourceid, TypeId, Sourceid, Context.User.Identity.Name, Path.GetFileName(FileUpload.PostedFile.FileName), phyiscalPath, "", New Byte(0) {}, FileUpload.PostedFile.ContentLength, "")
                    End If
                End If            If Not (File1.PostedFile Is Nothing) Then
                    If (File1.PostedFile.FileName <> "") Then
                        If Not File1.PostedFile.FileName.Trim().Length = 0 Then
                            Dim virtualPath As String = "~/uploads/" + Path.GetFileName(File1.PostedFile.FileName)
                            Dim phyiscalPath As String = Server.MapPath(virtualPath)
                            File1.PostedFile.SaveAs(phyiscalPath)
                            documents.UpdateDocument(Sourceid, TypeId, Sourceid, Context.User.Identity.Name, Path.GetFileName(File1.PostedFile.FileName), phyiscalPath, "", New Byte(0) {}, File1.PostedFile.ContentLength, "")
                        End If
                    End If
                End If            If Not (File2.PostedFile Is Nothing) Then
                    If (File2.PostedFile.FileName <> "") Then
                        If Not File2.PostedFile.FileName.Trim().Length = 0 Then
                            Dim virtualPath As String = "~/uploads/" + Path.GetFileName(File2.PostedFile.FileName)
                            Dim phyiscalPath As String = Server.MapPath(virtualPath)
                            File2.PostedFile.SaveAs(phyiscalPath)
                            documents.UpdateDocument(Sourceid, TypeId, Sourceid, Context.User.Identity.Name, Path.GetFileName(File2.PostedFile.FileName), phyiscalPath, "", New Byte(0) {}, File2.PostedFile.ContentLength, "")
                        End If
                    End If
                End If
                If Not (File3.PostedFile Is Nothing) Then
                    If (File3.PostedFile.FileName <> "") Then
                        If Not File3.PostedFile.FileName.Trim().Length = 0 Then
                            Dim virtualPath As String = "~/uploads/" + Path.GetFileName(File3.PostedFile.FileName)
                            Dim phyiscalPath As String = Server.MapPath(virtualPath)
                            File3.PostedFile.SaveAs(phyiscalPath)
                            documents.UpdateDocument(Sourceid, TypeId, Sourceid, Context.User.Identity.Name, Path.GetFileName(File3.PostedFile.FileName), phyiscalPath, "", New Byte(0) {}, File3.PostedFile.ContentLength, "")
                        End If
                    End If
                End If            If Not (File4.PostedFile Is Nothing) Then
                    If (File4.PostedFile.FileName <> "") Then
                        If Not File4.PostedFile.FileName.Trim().Length = 0 Then
                            Dim virtualPath As String = "~/uploads/" + Path.GetFileName(File4.PostedFile.FileName)
                            Dim phyiscalPath As String = Server.MapPath(virtualPath)
                            File4.PostedFile.SaveAs(phyiscalPath)
                            documents.UpdateDocument(Sourceid, TypeId, Sourceid, Context.User.Identity.Name, Path.GetFileName(File4.PostedFile.FileName), phyiscalPath, "", New Byte(0) {}, File4.PostedFile.ContentLength, "")
                        End If
                    End If
                End If
            Catch err As Exception
                Throw New Exception("出现错误", err)
            End Try    End Function
      

  21.   

    你看看
    Fileobj = CType(FindControl("FileUpload"), System.Web.UI.HtmlControls.HtmlInputFile)
    这样查出来的Fileobj是不是为空
      

  22.   

    必须处理PostedFile为空的异常,或者在上传时要判断是否为空,是为在页面加载时PostedFile是为空的
      

  23.   

    to:timmy3310(Tim) ( ) 问题解决,还是Form的enctype="multipart/form-data" 的问题,有一个页面正常是因为:有enctype="multipart/form-data",另外一个页面不正常是因为没有enctype="multipart/form-data",tmd,我拷贝时,一不小心把enctype="multipart/form-data"放到<body>里面去了,刚才检查代码时,发现enctype属性下面有红色波浪线,觉得奇怪,<form>明明有这个属性的,怎么会有“红线”呢?,仔细一看,才发现。。tmd,这简直是不可饶恕的错误。谢谢各位啊。。