why my fileupload.PostedFile is returning "" even when I have selected a file?

Partha Mandayam 91 Reputation points
2023-07-14T07:11:33.6066667+00:00

why my fileupload.PostedFile is returning "" even when I have selected a file?

PostedFile always returns the file selected so why this bizarre behavior?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,593 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Lan Huang-MSFT 30,176 Reputation points Microsoft Vendor
    2023-07-18T08:47:47.44+00:00

    Hi @Partha Mandayam,

    I think I should know what your problem is. (When you ask a question, it's best to provide an example of a minimal recurring problem so we can help you better)

    You need to set the Page.IsPostBack property to load the page in response to a client postback.

     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Page.IsPostBack Then
                SavetoDB()
            End If
        End Sub
        Public Sub SavetoDB()
    
            If image.HasFile Then
                Dim fileName As String = image.PostedFile.FileName
                ' Display or process the filename as needed
                Label1.Text = fileName
            End If
    
        End Sub
    

    enter image description here Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. AgaveJoe 29,781 Reputation points
    2023-07-18T15:44:12.43+00:00

    The HasFile flag is true when DovebidImageUploader() fires and false when cmdAdd_click fires. how did you check this? can you show your code?

    This is how I understand your code works.

    User control

    <%@ Control Language="vb" AutoEventWireup="false" CodeBehind="WebUserControl1.ascx.vb" Inherits="WebFormsVBDemo.WebUserControl1" %>
    <div>
    <div style="float:left;">
    
    <asp:FileUpload ID="image" runat="server" onchange="DovebidImageUploader();" Width="350px" Multiple="Multiple" />
        <span class="hint">
            <asp:Literal ID="Hint" runat="server" />
            <span class="hint-pointer"> 
            </span>
        </span>
    
    </div>
    </div>
    
    

    User control code behind. Set a break point on the single line of code in the Page_Load.

    Public Class WebUserControl1
        Inherits System.Web.UI.UserControl
    
        Public ReadOnly Property FileControl() As FileUpload
            Get
                Return Me.image
            End Get
        End Property
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim hasfile As Boolean = image.HasFile
        End Sub
    
    End Class
    

    The ASPX page with the JavaScript function that submits the page..

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebFormsVBDemo._Default13" %>
    
    <%@ Register Src="~/FileUpload/WebUserControl1.ascx" TagPrefix="uc1" TagName="WebUserControl1" %>
    
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Label ID="lblLockScnMsg" runat="server" Text="Label"></asp:Label>
            </div>
            <div>
                <uc1:WebUserControl1 runat="server" id="WebUserControl1" />
            </div>
            <div>
                <asp:Button ID="cmdAdd" runat="server" Text="Button" OnClick="cmdAdd_Click" />
            </div>
        </form>
        <script>
            function DovebidImageUploader()
            {
                //Added to stop Image download in Asset edit page.
                try { window.parent.DisableDnldImageFlag(); } catch (err) {}
    
    	            //$get('Log').style.top = (screen.height / 2).toString();
                 //   $get('Log').style.left = (screen.width / 2).toString();
                 //   $('#Log').show();
          
                    document.getElementById('<% =lblLockScnMsg.ClientID%>').innerHTML = "Processing [uploading images]....Please wait.....";
                    //$("#btnCancelImport").hide();
                    //$("#btnCancelUpload").hide();
                    //$find("mpeLockScreen").show();
    	            
                    document.forms[0].submit();
    	            //var par = window.parent.document;
    	            //var uploadstatus = par.getElementById('tdstatus');
    	            //uploadstatus.style.display = "";
            }
        </script>
    </body>
    </html>
    

    When an image is selected, the page posts back and image.HasFile is true. Click the cmdAdd button and image.HasFile is false as expected.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.