简体   繁体   English

如何从asp:FileUpload获取文件名并使用ASP.NET和C#显示在标签中

[英]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. 我正在使用<asp:FileUpload>将PDF文件上传到我的网页。 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 . 但是,单击浏览后,窗口打开,一旦我选择了一个文件并单击“ Open我想获取文件名并将其显示在“ Label What function should I use in ASP.NET to do this? 我应该在ASP.NET使用什么功能来做到这一点? I tried the OnLoad , OnUnload , OnDataBinding , etc. in the <asp:FileUpload> but nothing works. 我在<asp:FileUpload>尝试了OnLoadOnUnloadOnDataBinding等,但是没有任何效果。 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. 选择文件并单击打开后,文件名应显示在标签中。

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>

You can access the file name(server side) by using following code snippet 您可以使用以下代码段访问文件名(服务器端)

string file_name=fileUpload.FileName.ToString();

You can access the filename on client side using following code snippet 您可以使用以下代码段在客户端访问文件名

$(document).ready(function () {  

            $("#fileUpload").change(function () {  

                var FileUpload = $("#fileUpload").val();  
   ... }
 } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM