简体   繁体   English

ASP.Net控件选择文件(不上传)

[英]ASP.Net control to select a file (not upload)

I want to have a control which allows a user to click a link button, a file dialog comes up and the user selects a file. 我希望有一个允许用户单击链接按钮的控件,出现文件对话框,并且用户选择文件。 Currently I am using the FileUpload control as such: 目前,我正在使用FileUpload控件,例如:

<asp:LinkButton ID="btnDocumentLocation" runat="server" CausesValidation="False">Please Select...</asp:LinkButton>
<asp:FileUpload ID="fuDocumentLocation" style="display:none;" runat="server" ClientIDMode="AutoID" />

And in the code behind I have in Page_Load : 在我在Page_Load中的背后代码中:

btnDocumentLocation.OnClientClick = "$('[id*=\"" + fuDocumentLocation.ClientID + "\"]').click();";

This launches the (hidden) FileUpload control where you select your file. 这将启动(隐藏的) FileUpload控件,您可以在其中选择文件。 In Page_Load it checks the file as below and updates the Text of the LinkButton . Page_Load它检查文件如下,并更新LinkButtonText I do not need the actual file contents, just the file location: 我不需要实际的文件内容,只需文件位置:

if (IsPostBack && fuDocumentLocation.PostedFile != null && fuDocumentLocation.PostedFile.FileName.Length > 0)
{
    btnDocumentLocation.Text = fuDocumentLocation.PostedFile.FileName;
}
else
{
    btnDocumentLocation.Text = "";
}

This all works fine when ran locally. 在本地运行时,这一切都很好。 When published, the FileUpload seems to have an issue in the "uploading" part. 发布后, FileUpload在“上载”部分似乎有问题。 It doesn't post back to the server and therefore can't run the Page_Load code to populate the btnDocumentLocation.Text property. 它不会回发到服务器,因此无法运行Page_Load代码来填充btnDocumentLocation.Text属性。 I don't need it to grab the file, I just need the file location. 我不需要它来抓取文件,我只需要文件位置。

What I need to happen in order: 我需要发生的事情是:

  • User clicks on LinkButton 用户点击LinkButton
  • File dialog box pops up 弹出文件对话框
  • User selects a file 用户选择一个文件
  • LinkButton Text property set to full file name selected LinkButton Text属性设置为选定的完整文件名

Is there a control that allows for this without the overhead of getting the file data in the request? 是否有一个控件可以做到这一点,而又没有在请求中获取文件数据的开销? And will allow you to select files across different shares? 并允许您选择不同共享中的文件吗?

Why do you post back the page if you only want to change the text ? 如果只想更改文本,为什么还要发回页面? Instead, you can achieve the same without postback using Jquery/Javascript as mentioned below: 相反,您可以使用Jquery / Javascript实现相同的功能而无需回发,如下所述:

<asp:LinkButton runat="server" ID="btnDocumentLocation" OnClientClick="return GetFile();" >Browse...</asp:LinkButton>
<asp:FileUpload runat="server" ID="fuDocumentLocation" OnChange="ChangeText(this.value);" style="display: none" />


<script type="text/javascript">
    function ChangeText(selectedPath) {
        $('#' + '<%=btnDocumentLocation.ClientID%>').text(selectedPath);
    }

    function GetFile() {
        $('#' + '<%=fuDocumentLocation.ClientID%>').click();
        return false;
    }
</script>

If you want to use Pure JQuery then: 如果要使用Pure JQuery,则:

<asp:LinkButton runat="server" ID="btnDocumentLocation"  >Browse...</asp:LinkButton>
<asp:FileUpload runat="server" ID="fuDocumentLocation"  style="display: none" />


<script type="text/javascript">

    $(document).ready(function() {
        $('#' + '<%=btnDocumentLocation.ClientID%>').click(function () {
            $('#' + '<%=fuDocumentLocation.ClientID%>').click();
            return false;
        });

        $('#' + '<%=fuDocumentLocation.ClientID%>').change(function () {
            var selectedPath = $('#' + '<%=fuDocumentLocation.ClientID%>').val();
            $('#' + '<%=btnDocumentLocation.ClientID%>').text(selectedPath);

        });
    });
</script>
<asp:LinkButton runat="server" ID="btnDocumentLocation"  >Browse...</asp:LinkButton>
<asp:FileUpload runat="server" ID="fuDocumentLocation"  style="display: none" />


<script type="text/javascript">

$(document).ready(function() {
    $('#' + '<%=btnDocumentLocation.ClientID%>').click(function () {
        $('#' + '<%=fuDocumentLocation.ClientID%>').click();
        return false;
    });

    $('#' + '<%=fuDocumentLocation.ClientID%>').change(function () {
        var selectedPath = $('#' + '<%=fuDocumentLocation.ClientID%>').val();
        $('#' + '<%=btnDocumentLocation.ClientID%>').text(selectedPath);

    });
});

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

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