• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2381
  • Last Modified:

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
0
tdiscover
Asked:
tdiscover
  • 6
  • 5
  • 3
1 Solution
 
rafayaliCommented:
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)
0
 
Alex-w474Commented:
You cannot get the local file name or popup file browse dialog with regular HTML and JavaScript.
0
 
rafayaliCommented:
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.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Alex-w474Commented:
<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".
0
 
tdiscoverAuthor Commented:
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.
0
 
tdiscoverAuthor Commented:
I have upped the points as this is getting urgent.
0
 
tdiscoverAuthor Commented:
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
0
 
rafayaliCommented:
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.
0
 
tdiscoverAuthor Commented:
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.
0
 
rafayaliCommented:
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.
0
 
rafayaliCommented:
And here is the complete sample that I just created to re-verify my earlier comments.


In Web Form declarative syntax:

<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" style="width: 56px" Text="Button" />
    </div>
    </form>

In Code-behind, we have this:

 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Response.Write(FileUpload1.PostedFile.FileName)
    End Sub

After the above is run, there is NO UPLOAD.
0
 
tdiscoverAuthor Commented:
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
0
 
Alex-w474Commented:
rafayali, please let me know how to contact you directly. I want to clear the some aspects to you.
Thanks.
0
 
rafayaliCommented:
@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

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now