Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get file name from asp:FileUpload and display in a Label using ASP.NET and C#

I am using a <asp:FileUpload> to upload a PDF file to my web page. But after clicking on browse the window opens, and once I select a file and click on Open i want to get the file name and display it in a Label. What function should I use in ASP.NET to do this? I tried the OnLoad, OnUnload, OnDataBinding, etc. in the <asp:FileUpload> but nothing works. Can someone suggest me a solution for this?

My code is as below:

<asp:FileUpload ID="fileUpload" runat="server" /><br />
<asp:Label ID="labelFilename" runat="server" Text=""></asp:Label>

Once I select a file and click open the file name should be displayed in the label.

like image 433
Mano Prathibhan C Avatar asked Oct 26 '25 12:10

Mano Prathibhan C


1 Answers

You can use this code:

<script>
    $(document).ready(function () {
        $('#fileUpload').change(function () {
            var path = $(this).val();
            if (path != '' && path != null) {
                var q = path.substring(path.lastIndexOf('\\') + 1);
                $('#labelFilename').html(q);
            }
        });
    });
</script>
like image 91
Hamed Javaheri Avatar answered Oct 29 '25 02:10

Hamed Javaheri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!