Click here to Skip to main content
15,886,840 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have an asp.net fileupload control on my page. I use the following code to access the path and filename of the file selected:

string = file.PostedFile.FileName; //file is the fileupload control


When I run this webpage from the server, everything works fine - I get the filepath and filename, but when I run this webpage remotely, the PostedFile.FileName property loses the directory path and only gives the filename of the selected file, which is a problem because i need the path of the file in order to access it. Any help on how I can make the above work on remote logins just as on the server?
Posted
Comments
J a a n s 24-Aug-10 4:22am    
You don't need the path to 'access' it. You can use the FileBytes property in order to read the contents of uploaded file.
Hardus Lombaard 24-Aug-10 10:44am    
I was in a hurry for an answer and you saved me. Thanks a lot!

There is an interesting issue with FileUpload and IE browser.

Say, you have the following piece of code in the code behind:

string fileName = file.PostedFile.FileName;


If you upload the file from Firefox browser, you will get only the file name (Without any path) in the fileName variable (Say, AutoPlay.jpg)

But, if you upload the file from IE, you will get the File name along with the path in the user's computer from where the file is uploaded (Say, "C:\Users\shubho\Desktop\AutoPlay.png"). Note that, this path is not the path in the web server, this is the file path in client's PC.

Ideally, the file name should not contain the directory path and I don't know why IE does that (A bug?).

May be, while you are testing from your PC (Not from within the server), you are using a non-IE browser and hence, you are not seeing the file path.

Any way, you should code your program based upon the file name only, not based upon the file path of user's PC(That is sent by IE only).
 
Share this answer
 
Comments
Ankur\m/ 16-Mar-11 2:14am    
Newer IE browsers IE7+ takes care of this thing now. :)

If you are surprised how I got here, someone mentioned your answer's link in one of the similar question.

BTW haven't seen you around for quite sometime. Does that mean we have some wonderful articles coming?! :D
Al-Farooque Shubho 16-Mar-11 3:04am    
http://www.codeproject.com/KB/aspnet/WebSocketWhiteBoard.aspx :)
Before accessing the file, you need to save that file in the server. Upload that file to the server and access that file from server.
 
Share this answer
 
Comments
Member 13096697 18-Apr-17 4:05am    
How to upload file on server and accessfrom there?
The path you are refring to is the client side path where the file resides. In reality it is useless piece of information. What good is that to know where the file resided, unless you are working on intranet application.

I am maintaining piece of code in which the client side original path is saved as part of the file information. Now looking into it, I wonder what value it added to the application. Yeah! the file was uploaded from "c:\my documents\myfolder\myfiles\image.jpg" doh!.

All you need to know is the file name and once you upload the file you have a full control as to where you want it to live.
 
Share this answer
 

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