Link to home
Start Free TrialLog in
Avatar of tdiscover
tdiscover

asked on

getting a file path without uploading anything

Hi Experts,

I am using an input file field on my page so that the user can browse for a file. However I don't want to upload the file, I simply want the file path. How can I do this and still have the browse button?

Thanks,

Tdiscover
Avatar of rafayali
rafayali
Flag of Canada image

Are you using the FileUpload control to do this?

You can use FileUpload.PostedFile.Filename to access the fully qualified name of the file, which essentially returns the absolute path of the file.

For example, add a file upload control. Then, add a button. In button click event add this code to test:

 Response.Write(file1.PostedFile.FileName)
You cannot get the local file name or popup file browse dialog with regular HTML and JavaScript.
Alex, may be I have not fully understood your comment, but what about
<input id="File2" type="file" />

This is regular HTML (not server side). This generates file browse dialog box. And, this is just HTML.

<input type="file" id="file">
<button onclick="alert(document.getElementById('file').value)">Get path</button>

Try this code in FireFox and Opera - they return only file name.
IE and Safari (for windows) return the full path.

By "file browse dialog" I mean the dialog itself, not part of input type="file".
Avatar of tdiscover
tdiscover

ASKER

Thanks for your comments. Will these solutions you have given return only the file path when the form is submitted? My problem is that when the form is submitted, the file is uploaded too.
I have upped the points as this is getting urgent.
Ok, I have solved this one myself. The solution is to use an htmlInput control and NOT an ASP control. Make sure you don't set a name attribute or it will still upload the file. Also create a javascript function that runs when the user submits the form. What the javascript function is to do is get the file path by document.getElementByID("fileID").value and put it in a hidden field - then in the server code you get the file path via the hidden field.value

Thanks for your comments Experts
The solution that I had provided did solve the user's problem which is NOT TO HAVE A FILEUPLOAD. The first comment from my side provided this code - nonetheless, the poster also did find an alternate way of going about it.

My code provides the path as well (the very first comment/reply in this thread). Also, IT DOES NOT UPLOAD THE FILE EITHER (as the poster requested). Because, Fileupload control uses SaveAs() method to upload file.

Anyhow, I object to points refund on the grounds that my solution works and it was provided before the user got back to us saying that he has found an alternate way. There are always alternate ways of doing things and as such every user can come back at a later time with alternate solutions and request a refund of points. That is totally unfair to experts who spent time on the question.
rafayali, I have tried your solution. And I just re-tried to make sure. But it still uploads the file - my browser sits for a minute or so as it uploads the file. Sure your solution gave me the filepath but my question was how do I get the filepath without uploading a file. I know you understand this and you say that the file does not upload because it uses SaveAs() method - but I have just tested your solution and it did still upload the file. I'd be more than happy to give you all the points if your solution worked, even though I have come up with an alternative one myself.
tdiscover, thanks for your reply.I dont know how and why, but direct upload without calling SaveAs() on fileupload control is JUST NOT POSSIBLE. Have you double checked to make sure that you are not calling this method?

Here is an excerpt from the Microsoft MSDN documentation

"The FileUpload control does not automatically save a file to the server after the user selects the file to upload. You must explicitly provide a control or mechanism to allow the user to submit the specified file."

This documentation may be read at: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.aspx

As you can see, direct uploading is JUST NOT POSSIBLE.

I tested this solution at my end as well after reading your comment and there was NO UPLOAD PERFORMED.

Please let me know what code you executed so that I can look at it. I have no objection to points being refunded if my solution DOES NOT WORK, BUT I have just tried my solution and it works. Plus, MSDN documentation specifically states that uploading is NOT POSSIBLE unless SaveAs() is called. So, I dont know what exactly is causing this to upload, but it definitely has nothing to do with the solution that I provided.
ASKER CERTIFIED SOLUTION
Avatar of rafayali
rafayali
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, I have used the same code that you just posted there. But it didn't work for me... perhaps something else on my page is causing a problem. You obviously know your stuff and researched (msdn) and I thank you for that. So, I'll look into it futher over the coming days as I find time and try get your solution working as it would be cleaner than my solution. You certainly deserve the points, again, thanks
rafayali, please let me know how to contact you directly. I want to clear the some aspects to you.
Thanks.
@tdiscover:

thanks tdiscover. This is quite strange. If you want, you can email me a part of your code and I can review what you are trying to accomplish. Sometimes a good old codebehind file is better than description of the problem and it is also easier to see what is being done. So, you can email me at rafayali [at] gmail.com if you have any further concerns with this.

@Alex-w474
You can contact me directly at rafayali [at] gmail.com