Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.Net控件选择文件(不上载)_C#_Asp.net_File Upload - Fatal编程技术网

C# ASP.Net控件选择文件(不上载)

C# ASP.Net控件选择文件(不上载),c#,asp.net,file-upload,C#,Asp.net,File Upload,我想有一个控件,允许用户点击链接按钮,一个文件对话框出现,用户选择一个文件。目前我使用的控件如下所示: <asp:LinkButton ID="btnDocumentLocation" runat="server" CausesValidation="False">Please Select...</asp:LinkButton> <asp:FileUpload ID="fuDocumentLocation" style="display:none;" runat=

我想有一个控件,允许用户点击链接按钮,一个文件对话框出现,用户选择一个文件。目前我使用的控件如下所示:

<asp:LinkButton ID="btnDocumentLocation" runat="server" CausesValidation="False">Please Select...</asp:LinkButton>
<asp:FileUpload ID="fuDocumentLocation" style="display:none;" runat="server" ClientIDMode="AutoID" />
这将启动隐藏文件上载控件,您可以在其中选择文件。在Page_Load中,它检查文件如下,并更新LinkButton的文本。我不需要实际的文件内容,只需要文件位置:

if (IsPostBack && fuDocumentLocation.PostedFile != null && fuDocumentLocation.PostedFile.FileName.Length > 0)
{
    btnDocumentLocation.Text = fuDocumentLocation.PostedFile.FileName;
}
else
{
    btnDocumentLocation.Text = "";
}
这一切在本地运行时都可以正常工作。发布时,文件上载在上载部分似乎有问题。它不会发回服务器,因此无法运行页面加载代码来填充btnDocumentLocation.Text属性。我不需要它来抓取文件,我只需要文件的位置

我需要做的是:

用户点击链接按钮 “文件”对话框弹出 用户选择一个文件 LinkButton文本属性设置为选定的完整文件名
是否有一个控件允许这样做,而不需要在请求中获取文件数据的开销?并允许您选择不同共享的文件?

如果您只想更改文本,为什么要发回页面?相反,您可以使用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>
如果要使用纯JQuery,请执行以下操作:


使用jquery只显示这个简单的文件名看看我的答案。希望你能得到你想要的。这是最接近我正在寻找的,但在选择文件时,FileUpload控件仍然会发回服务器。这意味着它仍将所选文件作为请求发送,如果是大文件,则可能需要一段时间才能发回。这是我最初发布的问题……相反……因为它适用于不在本地服务器上的用户,但如果您尝试从本地服务器访问它,则在文件上载时可能会面临漫长的等待。有什么方法可以阻止回发邮件的传播吗?@另一个灌木林:你能告诉我它什么时候回发页面吗?因为我不认为在选择文件时,您的页面会收到回帖。当我从“文件上载”对话框中选择一个文件时,它会通过jQuery更改链接按钮文本,然后回帖到页面。@Anothershubery:在调用GetFile之前,请确保您已经写了return;方法并且在JavaScript中已经编写了returnfalse方法。并删除您在页面加载事件中为btnDocumentLocation的OnClientClickI编写的代码,该代码在错误的位置返回false!谢谢你,现在就开始工作!
<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>
<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);

    });
});