How to submit a file's original path and filename without submitting the file itself in ASP.NET?

StackOverflow https://stackoverflow.com/questions/625524

  •  05-07-2019
  •  | 
  •  

Question

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)?

Was it helpful?

Solution

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top