Wednesday, August 20, 2008

How to get the full path of a file from a HTML File Input

This could also be modified for use in an ASP.Net FileUpload. I was making an internal intranet site where I wanted a user to choose a file, and then my ASP.Net application would use the path to generate categories etc (ie if its in the Columns folder, the file is a Column). Unfortunately ASP.Net does not allow you to retrieve the whole path (ie C:\Columns\Test.rfa) from a file upload control, it only will show you the actual file name (Test.rfa). To get around this, I made use of Javascripts OnChange event. First, on the <input type="file"> I added "onchange="CapturePath(this.value);" Then, at the top of my page I added a javascript function which does the following: function CapturePath(value) { document.getElementById("<%Response.Write(filePathHidden.UniqueID);%>").value = value; } As I was using, I wanted to store the path for postback to the server, so I added an control, called 'filePathHidden', and then when the user changes the value of the File Input, it sends the path to this hidden. You could modify the code to use a normal HTML Hidden as well. Then, on post back, I get the value of the hidden and use it as I please.


Tho said...

Thanks for this. Works well in IE, but not in Firefox. I guess something is better than nothing.

Anonymous said...

I am trying to put the full document location path in the footer of that document. I am not a Developer so I am looking at creating this in label format - can anyone tell me what properties I should put in the label. Example, for Version I typed {Version}, file path would be {???}.