Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi, i want to retrieve the file path of a file when it is being uploaded. When the user clicks browse and selects a file from his computer, i want to be able to retrieve the local path to this file even before it is uploaded. The code am using is below:

C#
string filePath = null;
           //this gets the file location of the excel sheet
           if (uploadFile.ContentLength > 0)
           {
              filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), Path.GetFileName(uploadFile.FileName));



           }


it does not seem to work because the file has to be in the Upload folder first. meanwhile the user is suppose to select the file from any location and the system should retrieve the path. Any help is most appreciated
Posted
Comments
Ed Nutting 25-Aug-11 11:08am    
Please see: http://www.codeproject.com/KB/aspnet/fileupload.aspx or a similar article if you don't like this one. You need to understand the difference between client side and server side files/script. It scares me that you appear not to understand this basic concept.
Dave Kreskowiak 25-Aug-11 11:13am    
Sadly, there is no voting on comments, otherwise I would give you a 5 for this one.
[no name] 25-Aug-11 11:15am    
I must be interrupting it wrong. I don't see this as being helpful for the question that was asked
Dave Kreskowiak 25-Aug-11 13:23pm    
It's obvious he doesn't know the difference between the two from his code snippet. Edward is correct, this guy needs to understand the differences before spelunking around with guesswork code that he doesn't understand the workings of.
[no name] 25-Aug-11 20:26pm    
Sorry I don't see it as guess work code and spelunking about. I see it as someone trying to learn.

A user can select a file for upload from any path they have access to, however, because of browser security you will not be able get this path, only the filename in the code-behind
 
Share this answer
 
v2
Comments
[no name] 25-Aug-11 11:19am    
You can get the path on the client side, store it in a hidden field and pass it back to the server.
[no name] 25-Aug-11 11:27am    
I have had the cutesy to not make any further comments to you or about or bother you in the least. Do the same!
[no name] 25-Aug-11 11:51am    
Pointing out a real mistake is not really a problem. I have even apologized. Let us be professional Mark. I have nothing against you personally.
[no name] 25-Aug-11 12:00pm    
"I have nothing against you personally"
Yet, you called me pervert, moron and suggested I need psychological help. How is that not personal? As I said, if you want to truly and sincerely apologize then do it in the lounge, where these comments were made.

Otherwise, stay away or be reported as abusive, again.
[no name] 25-Aug-11 12:03pm    
Apology is personal and I have done it. If you want to make a publicity out of it, then I'm not for the game. You fail to see the other side of the story.
You code-behind code executes "after" the file is uploaded, so there is no way you can do it using C#. You may try to do it from jQuery like this:
JavaScript
var filename = $("#fileUploadControl").val();


[EDIT]As I can see, this solution works in IE and not in Firefox. Firefox returns the file name alone. Haven't tested this in other browsers.[/EDIT]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900