FileUpload1.HasFile is False

2019-02-26 21:25发布

问题:

I tried to search for the problem in the internet and I see everyone is asking about the problem for FileUpload control inside the UpdatePanel. First of all, I am not using an UpdatePanel. Below is my code:

HTML

<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" method="post" runat="server" enctype="multipart/form-data">
    <div>
        <asp:FileUpload ID="fuImport" runat="server" />
        <asp:Button ID="btnImport" runat="server" Text="Import" />
    </div>
    </form>
</body>
</html>

Code Behind

Protected Sub btnImport_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnImport.Click
    If (fuImport.HasFile) Then
        fuImport.SaveAs(My.Settings.FileImportPath & Path.GetFileName(fuImport.FileName))
    End If
End Sub

I see that fuImport.HasFile is False, but fuImport.FileName gives just the file name. For e.g., if I choose c:\1.txt, it gives just "1.txt". Can anybody let me know why fuImport.HasFile is False though I have chosen a file?

回答1:

I found the answer. The txt file I was uploading was empty. I edited the text file and then saved by typing something in it. I could not find this anywhere mentioned in the msdn or I am not sure whether I was looking at a wrong place. The suggestion by Kasys in this post helped me.