Why I can't get selected file on server side?

Michael

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"];
Aristos

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why I can't get selected file on server side?

From Dev

Why can't I get two <p> elements to be side-by-side and aligned by center?

From Dev

Why can't server side and client side scripts interact?

From Dev

why can't I hold three boxes side-by-side?

From Dev

Why can't I get netcat talking to my Rails server?

From Dev

FlaskApp Running with Apache unable to save file on server side can't get permission to work out?

From Dev

How can I get a posted file on server-side from an Ajax Request in asp.net-webpages?

From Dev

Why can't I get the file name under the if statement?

From Dev

Why can't the selected files be posted to server client when I use Modal form of jQuery UI?

From Dev

Can't open the excel file after generating it on server side

From Dev

Can't read property file on Server side with GWT

From Dev

Can I get location of android device without GPRS on server side?

From Dev

I Can't read from the server-side socket

From Dev

Why I can't place text in parallelogram with one side

From Dev

ListView - Why can't I see any selected items?

From Dev

Why can't I duplicate selected items in for loop?

From Dev

MIPS Why I can't print the selected array position?

From Dev

Why can't I get $_FILES[“file”][“tmp_name”] when uploading a zip file?

From Dev

I can't figure out why I get a blank output file

From Dev

Can't Get Divs to render Side By Side

From Dev

Why can't I write to the file mmaped

From Dev

Why can't I read whole file?

From Dev

Why can't I empty the file in this way?

From Dev

Why can't I delete this file?

From Dev

Why can't I delete this file as root?

From Dev

Why can't I write to the file mmaped

From Dev

Why can't I read whole file?

From Dev

Why can't I empty the file in this way?

From Dev

Why I can't read file?

Related Related

  1. 1

    Why I can't get selected file on server side?

  2. 2

    Why can't I get two <p> elements to be side-by-side and aligned by center?

  3. 3

    Why can't server side and client side scripts interact?

  4. 4

    why can't I hold three boxes side-by-side?

  5. 5

    Why can't I get netcat talking to my Rails server?

  6. 6

    FlaskApp Running with Apache unable to save file on server side can't get permission to work out?

  7. 7

    How can I get a posted file on server-side from an Ajax Request in asp.net-webpages?

  8. 8

    Why can't I get the file name under the if statement?

  9. 9

    Why can't the selected files be posted to server client when I use Modal form of jQuery UI?

  10. 10

    Can't open the excel file after generating it on server side

  11. 11

    Can't read property file on Server side with GWT

  12. 12

    Can I get location of android device without GPRS on server side?

  13. 13

    I Can't read from the server-side socket

  14. 14

    Why I can't place text in parallelogram with one side

  15. 15

    ListView - Why can't I see any selected items?

  16. 16

    Why can't I duplicate selected items in for loop?

  17. 17

    MIPS Why I can't print the selected array position?

  18. 18

    Why can't I get $_FILES[“file”][“tmp_name”] when uploading a zip file?

  19. 19

    I can't figure out why I get a blank output file

  20. 20

    Can't Get Divs to render Side By Side

  21. 21

    Why can't I write to the file mmaped

  22. 22

    Why can't I read whole file?

  23. 23

    Why can't I empty the file in this way?

  24. 24

    Why can't I delete this file?

  25. 25

    Why can't I delete this file as root?

  26. 26

    Why can't I write to the file mmaped

  27. 27

    Why can't I read whole file?

  28. 28

    Why can't I empty the file in this way?

  29. 29

    Why I can't read file?

HotTag

Archive