Get full path of a file with FileUpload Control

2019-01-01 05:37发布

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?

18条回答
泛滥B
2楼-- · 2019-01-01 06:06

Just to give my 2 cents.

At this moment I also DO get the full user's local path. It's only from 1 machine that I can replicate this issue, but it really does give the full path of the file at the machine of the user.

This is a end-user of our application which is hosted on a off-site server. So it's not on the local machine nor on a local server from which it might happen to be a share.

You can resolve the issue, at least to have the same behaviour all the time by this:

Path.GetFileName(fileUpload.FileName)

Btw, just found this article which states it can happen too: http://www.eggheadcafe.com/community/aspnet/17/10092650/fileupload-control-pro.aspx

Just to confirm the issue.

查看更多
看淡一切
3楼-- · 2019-01-01 06:08

This dumps the file in your temp folder with file name, then after that you can call it and not worry about it. Because it will get deleted if it is in your temp folder for an amount of days.

string filename = Path.Combine(Path.GetTempPath(), Path.ChangeExtension(Guid.NewGuid().ToString(),".xls"));
                File.WriteAllBytes(filename, FileUploadControl.FileBytes);
查看更多
后来的你喜欢了谁
4楼-- · 2019-01-01 06:09

As of IE8, the full path is no longer sent to sites in the Internet Zone.

See the "File Upload Control" section at the end of this post: http://blogs.msdn.com/ie/archive/2008/07/02/ie8-security-part-v-comprehensive-protection.aspx for discussion.

查看更多
伤终究还是伤i
5楼-- · 2019-01-01 06:09

Check this post under FileUpload Control

Additionally, the “Include local directory path when uploading files” URLAction has been set to "Disable" for the Internet Zone. This change prevents leakage of potentially sensitive local file-system information to the Internet. For instance, rather than submitting the full path C:\users\ericlaw\documents\secret\image.png, Internet Explorer 8 will now submit only the filename image.png.

Its an option under Internet security that can be enabled

查看更多
情到深处是孤独
6楼-- · 2019-01-01 06:15

On Internet Explorer Options, on security tab click on custom security button, there have an option to send the local path when loading some file to server.

Disabled as default. Just enable it.

查看更多
其实,你不懂
7楼-- · 2019-01-01 06:16

You can't get full path of a file at client's machine. Your code might work at localhost because your client and the server is the same machine and the file is at the root directory. But if you run it on a remote machine you will get an exception.

查看更多
登录 后发表回答