Solved

getting a file path without uploading anything

Posted on 2008-06-18
15
2,370 Views
Last Modified: 2013-12-16
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
Comment
Question by:tdiscover
  • 6
  • 5
  • 3
15 Comments
 
LVL 6

Expert Comment

by:rafayali
ID: 21818363
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
 
LVL 5

Expert Comment

by:Alex-w474
ID: 21818458
You cannot get the local file name or popup file browse dialog with regular HTML and JavaScript.
0
 
LVL 6

Expert Comment

by:rafayali
ID: 21818479
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
 
LVL 5

Expert Comment

by:Alex-w474
ID: 21818551
<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
 

Author Comment

by:tdiscover
ID: 21818887
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
 

Author Comment

by:tdiscover
ID: 21818980
I have upped the points as this is getting urgent.
0
 

Author Comment

by:tdiscover
ID: 21819403
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 6

Expert Comment

by:rafayali
ID: 21819768
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
 

Author Comment

by:tdiscover
ID: 21827075
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
 
LVL 6

Expert Comment

by:rafayali
ID: 21827136
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
 
LVL 6

Accepted Solution

by:
rafayali earned 500 total points
ID: 21827140
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
 

Author Comment

by:tdiscover
ID: 21827179
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
 
LVL 5

Expert Comment

by:Alex-w474
ID: 21827249
rafayali, please let me know how to contact you directly. I want to clear the some aspects to you.
Thanks.
0
 
LVL 6

Expert Comment

by:rafayali
ID: 21827275
@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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now