fearofawhackplanet
fearofawhackplanet

Reputation: 53388

ASP.NET file browser get filepath

I'm using an <input type="file" /> as a file browser dialog in MVC. I don't actually want to upload the file though, I am using it to select a template on a shared drive. I only want to get the full filepath in my action method. The server will then process the file and force download to the client. I have got hold of the HttpPostedFileBase object, but I don't see a filepath property. Is this possible? Or if not, what other options are available?

Upvotes: 1

Views: 3033

Answers (2)

Brian Dishaw
Brian Dishaw

Reputation: 5825

You could try something like adding an onchange javascript event on the input and write the value to a hidden field. Then just read in the value from the hidden field. It will contain everything that's in the text box part of the input field.

<input type="file" name="fileUploader" id="fileUploader" onchange="filePath.value = fileUploader.value;" />
<input type="hidden" name="filePath" id="filePath" />

I haven't looked at it with MVC but in ASP.Net I'm able to read that value from the code behind after using the input field.

Hope this helps.

Upvotes: 0

rakuo15
rakuo15

Reputation: 889

You will never be able to get the full path of a file from a file upload box because it is a security issue. No browser will pass you the full path, only the filename.

Additionally, the file upload box will always upload the file. Unfortunately, much like with the security issue of the full path, there is no way to show a file dialog and have the user pick a file without initiating an upload.

Upvotes: 1

Related Questions