AHMAD
AHMAD

Reputation: 95

File Upload Issue - how to fix?

I'm trying to upload file in tow scenarios

First:

<input  id="File2" runat="server" name="name" type="file" clientidmode="Static"  />
<asp:Button ID="Button4" runat="server"  clientidmode="Static" 
  Text="Go CodeBehind To Get Input Value" OnClick="btnUploadClick" />

This works correctly and the postedFile not null in code behind C#

protected void btnUploadClick(object sender, EventArgs e)
{
    HttpPostedFile postedFile= Request.Files[0];
}

Second:

I want to change the browse button "text" and I know already , can't do that directly so i worked around it like that:

<b>
    <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"  >
    <input  id="File1" runat="server" name="name" type="file" clientidmode="Static" onchange="setHiddenValue()" style=" visibility:hidden;" />
        <br />
        <input id="Button2" type="button" clientidmode="Static" onclick="triggerFileUpload()" value="HTML Button" />
        <br />
        <asp:Button ID="Button3" runat="server"  clientidmode="Static" Text="Go CodeBehind To Get Input Value" OnClick="btnUploadClick" />

        <script language="javascript">
            function triggerFileUpload() {
                document.getElementById("File1").click();
            }
        </script>
    </asp:Content>
<b>


protected void btnUploadClick(object sender, EventArgs e)
{
    HttpPostedFile postedFile= Request.Files[0];
}

When I press the Button2 the fileDialog open, I select file and everything Ok.

But when I press Button3 to get file in server side c# the Request.Files[0] is null and Found no file posted.

I want the Request.Files[0] because I want save it in database as byte

so please if u have any idea I'll appreciate it

Thank You in advance

Upvotes: 0

Views: 358

Answers (1)

Wagner Leonardi
Wagner Leonardi

Reputation: 4446

I did the same code (second scenario) in a blank project, and this works fine. You could do the same and check it.

Probably something around this slice of code is breaking this. Check if masterpage or current page Page_Load() method have something that break the file 'postback'. You can try:

protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)
   {
      //Page_Load code
   }
}

Upvotes: 1

Related Questions