How to submit a file's original path and filen

2019-06-11 06:03发布

How can I have a control on a ASP.NET form that browses for files as FileUpload control does, but instead of submmitting the whole file, it only submits the path from witch the file was found from (with the original file name)?

1条回答
beautiful°
2楼-- · 2019-06-11 06:41

Here is a workaround I made by following the instructions of the following forum post: http://forums.asp.net/p/1189182/2040139.aspx#2040048

<asp:FileUpload ID="File1" runat="server" onchange="GetFileName();"/> 
<asp:Button ID="Submit" runat="server" Text="Submit" OnClientClick="DisableFileSelector();" />
<asp:HiddenField id="txtFileName" runat="server" />

<script language="javascript" type="text/javascript">
    function GetFileName()
    {
        document.getElementById('<%=txtFileName.ClientID %>').value = document.getElementById('<%=File1.ClientID %>').value;
    }
    function DisableFileSelector() {
        document.getElementById('<%=File1.ClientID %>').disabled = true;
    }
</script>

Of course, this only works if JavaScript is enabled. If the JavaScript is disabled, the form will submit the file, but you can still perserve the functionality of the site by adding the following code before the first time txtFileName is being use.

if (File1.HasFile)
{
    txtFileName.Value = File1.PostedFile.FileName;
}
查看更多
登录 后发表回答