1

I have this asp code:

        <asp:Panel runat="server">  
            <div class="row2">
            <input type="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" id="fileSelect" name="fileSelect" runat="server" />
            <asp:Button ID="btnUpload" runat="server" Text="load" OnClick="btnUpload_Click2" CausesValidation="False" />
            </div>
        </asp:Panel>

Here the generated HTML code on the browser:

<div class="row2">               
            <input name="ctl00$ContentPlace$fileSelect" type="file" id="ctl00_ContentPlace_fileSelect" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel">
            <input type="submit" name="ctl00$ContentPlace$btnUpload" value="load" id="ctl00_ContentPlace_btnUpload">
        </div>
</div>

Here the code behind:

    protected void btnUpload_Click2(object sender, EventArgs e)
    {
        HttpPostedFile file = Request.Files["fileSelect"];                     
    }

After I select a file with help of input file element the btnUpload is pressed the code behind fired but I always get file value null.

If I change this row:

HttpPostedFile file = Request.Files["fileSelect"];

To this row:

HttpPostedFile file = Request.Files["ctl00$ContentPlace$fileSelect"]; 

I get the selected file in file variable on the server.

So my question is why I can't get file if I use this row:

HttpPostedFile file = Request.Files["fileSelect"];
Michael
  • 13,950
  • 57
  • 145
  • 288
  • If you inspect the resulting HTML on the client side, what is the value of the name attribute on your file upload element? – mason Feb 28 '16 at 01:10

1 Answers1

1

Your input control that runs at server side, change their id, and to get the name that is posted you need to use the UniqueID as

HttpPostedFile file = Request.Files[fileSelect.UniqueID];

where in your case the fileSelect.UniqueID is return "ctl00$ContentPlace$fileSelect" that is the nane rendered control on the html, the one that used on post.

you can see also Accessing control client name and not ID in ASP.NET

How to use what you type

To use that line

HttpPostedFile file = Request.Files["fileSelect"];

just remove the runat="server" from your input control.

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150