I am working on a web application which uses the FileUpload control. I have an xls file in the full filepath 'C:\Mailid.xls' that I am attempting to upload.
When I use the command
FileUpload1.PostedFile.FileName
I cannot get the full filepath from my system. However, when I use the above command in another system it works fine.
I also tried the following commands with no success:
System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName);
Path.GetFileName(FileUpload1.PostedFile.FileName);
System.IO.Path.GetDirectoryName(FileUpload1.PostedFile.FileName).ToString();
Convert.ToString(System.IO.Directory.GetParent(FileUpload1.PostedFile.FileName));
How can I get full path?
Check this:
Code:
I had sort of the opposite issue as the original poster: I was getting the full path when I only wanted the filename. I used Gabriël's solution to get just the filename, but in the process I discovered why you get the full path on some machines and not others.
Any computer joined to domain will give you back the full path for the filename. I tried this on several different computers with consistent results. I don't have an explanation for why, but at least in my testing it was consistent.
FileUpload will never give you the full path for security reasons.
This will not problem if we use IE browser. This is for other browsers, save file on another location and use that path.
if (FileUpload1.HasFile)
{
string fileName = FileUpload1.PostedFile.FileName;
string TempfileLocation = @"D:\uploadfiles\";
string FullPath = System.IO.Path.Combine(TempfileLocation, fileName);
FileUpload1.SaveAs(FullPath);
Response.Write(FullPath);
}
Thank you
By the way, I am using Visual Studio 2010. I don't know if there is a difference with the other version.