45

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?

Ray
  • 169,974
  • 95
  • 213
  • 200
Dhanraj
  • 499
  • 1
  • 5
  • 14
  • In what language are you workin? What exactly you need to do, and how? I ddidnt understand the question. – Strae Jul 15 '09 at 10:22
  • 1
    You need to provide more information (edit your post). Language, platform what your error message are. And please format your code using the editors code button. – Nifle Jul 15 '09 at 10:26

18 Answers18

31

It's currently true that "when you upload a file the browser will only send the source filename and not the full path" - it makes perfect sense that the server has no business knowing whether the file was in "C:\WINDOWS\" or "F:\SOMEDIR\OTHERDIR\PERSONALINFO\". The filename is always sent, and is useful both to help the user 'recognise' the content and possibly to interrogate the file extension to help determine the file type.

However I know from experience that Internet Explorer definitely used to (in older versions) send the entire path. It's difficult to find an authoritative confirmation (except this apache fileupload control doco)

Internet Explorer provides the entire path to the uploaded file and not just the base file name

Regardless, you should not use nor expect the full path to be sent by any 'modern' browser.

Conceptdev
  • 3,251
  • 1
  • 19
  • 22
  • 10
    I would also add that you should never assume the .FileName property will only ever contain just the filename and nothing else. Sometimes it can contain the fully quanlified path, most times just the filename. It depends on how old the client browser is. In any case you should ALWAYS wrap Path.GetFileName(fileUpload.FileName) in order to guarantee it's definitely the filename you;re dealing with and NOT the path. (Path utility class is in System.IO namespace). – Sunday Ironfoot Mar 12 '10 at 10:46
  • 6
    Internet Explorer 8 has been fixed to only provide the filename in the Internet zone, but in the Local Intranet zone, it still sends the full path. +1 to @Sunday Ironfoot for the Path.GetFileName suggestion. – Richard Fawcett May 17 '11 at 14:01
23

Perhaps you misunderstand the way FileUpload works.

When you upload a file, it is effectively being transferred from the client's computer to the server hosting your application. If you're developing the application, most times, both client and server are the same machine (your computer). Once the application is deployed however, there could be any number of clients connecting to the server, each uploading a different file.

Knowing the full path of the file on the client's computer usually isn't necessary - you'll often want to do something with the file contents. Your examples seem like ASP.NET C#, so I'm guessing you're using the FileUpload control. You can get at the uploaded file's contents by reading the raw stream (FileUpload.PostedFile.InputStream) or by saving the file first (FileUpload.PostedFile.SaveAs), then accessing the saved file. It's your responsibility to save the file, if you want it to be accessible after the current request - if you don't, ASP.NET deletes it.

One more thing - don't forget to set the enctype property on your form to "multipart/form-data". If you don't, the client's browser won't send the file, and you'll spend quite a few minutes wondering what went wrong.

elo80ka
  • 11,979
  • 3
  • 33
  • 43
  • am developing an web application. I am using file upload control to browse files.It was working fine. Today I installed IE 8 After the installation I unable to get the full path of the file what could be the problem Event I uninstalled IE 8. Currently I am using IE 7. – Dhanraj Jul 15 '09 at 11:07
  • 1
    this worked for me: `FileUpload.PostedFile.FileName` thanks elo80ka – Somebody Nov 03 '12 at 23:18
  • 1
    That was it. Forgot the basic functionality of that control. – Stanton May 02 '14 at 22:52
  • This is how the control works .. this should be marked as the correct answer. – AltF4_ Jun 20 '18 at 10:04
9

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.

EricLaw
  • 54,427
  • 7
  • 140
  • 182
9

Try

Server.MapPath(FileUpload1.FileName);

Edit: This answer describes how to get the path to a file on the server. It does not describe how to get the path to a file on the client, which is what the question asked. The answer to that question is "you can't", because modern browser will not tell you the path on the client, for security reasons.

criticalfix
  • 2,780
  • 1
  • 16
  • 32
Abdul Wahhab
  • 386
  • 2
  • 2
  • 20
    This wont work. Assuming the application root is C:\MySource\MyProject\ then Server.MapPath("Mailid.xls") will give you C:\MySource\MyProject\Mailid.xls instead of C:\Mailid.xls as the question said was needed. – daniloquio Sep 06 '11 at 18:20
  • 2
    Sorry, this doesn't actually address the original issue. – Walden Leverich Jan 17 '13 at 18:02
  • 2
    It is wrong answer. Server.MapPath() return application virtual directory path. – Ganesha Dec 05 '13 at 17:03
  • 1
    This is wrong answer why it has up votes down vote it – MohammadMMohammad Apr 18 '14 at 08:52
  • 1
    This wrong answer currently has 12 downvotes and 23 upvotes. I have attempted to flag it because it doesn't answer the question. The flag was declined. Apparently we are not supposed to flag wrong answers. Please, will people stop upvoting this answer, or at least explain why they think it is a correct answer to the question? – criticalfix Jul 23 '14 at 21:48
  • I have edited the answer in an attempt to clarify the distinction between what the question asked and what the answer answered. I am guessing the upvotes are from people who actually want to find the file path on the server, not the client. So, for them, this answer may have value, even though it is unrelated to the question. – criticalfix Jul 24 '14 at 15:19
  • 1
    Server.MapPath show the file path of the application. If you want to get the upload file path, use FileUpload1.SaveAs method, save the upload file to your server path fileUpload.SaveAs(Server.MapPath("~/Files/")) on the folder named Files. Then you can access the file – charulatha krishnamoorthy Dec 04 '14 at 13:03
  • This post doe not address the original issue asked – AltF4_ Jun 20 '18 at 09:50
8

FileUpload will never give you the full path for security reasons.

waqasahmed
  • 3,198
  • 5
  • 27
  • 50
  • hi thanks for u r reply. But that command showing the full path in another system. how it's possible. – Dhanraj Jul 15 '09 at 10:42
  • Using the fileUpload, you cannot get the full path of the client machine of the file. No way. The FileUpload is able to handle uploading the file, and you don't need to worry about the path. You may have got the full path of a file via other means (e.g System.IO.Path or MapPath or something else). But I doubt you used FileUpload to get the full path. – waqasahmed Jul 15 '09 at 11:53
8
Convert.ToString(FileUpload1.PostedFile.FileName);
DarthJDG
  • 16,140
  • 11
  • 47
  • 55
Santosh Lodhi
  • 89
  • 1
  • 2
3

IE 7 and previous versions sent the full path of the uploaded file to the server related to the input type="file" field. Firefox and other modern browsers consider this to be a security flaw and do not. However, this appears to be have been fixed in IE 8.

Perhaps you should instead evaluate why you need the full path to the file as it was located on the client's system. I think it is really superfluous information that should never be posted at all. All you should be concerned with the is the file name so that you can save the file without making changes to the name.

Cerebrus
  • 25,080
  • 8
  • 54
  • 70
3

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.

Gabriël
  • 1,213
  • 1
  • 21
  • 30
3

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.

BrianM
  • 538
  • 1
  • 3
  • 8
3

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);
Flexo
  • 82,006
  • 22
  • 174
  • 256
3

Check this:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="FileUp.aspx.vb" Inherits="FileUp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server"></asp:Label><br />
       <asp:FileUpload ID="FileUpload1" runat="server" /><br />
        <asp:Button ID="Button1" runat="server" Text="Upload" />
    </div>
    </form>
</body>
</html>

Code:

Partial Class FileUp
    Inherits System.Web.UI.Page
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim path As String
        Dim path1 As String
        path = Server.MapPath("~/")
        FileUpload1.SaveAs(path + FileUpload1.FileName)
        path1 = path + FileUpload1.FileName
        Label1.Text = path1
        Response.Write("File Uploaded successfully")
    End Sub
End Class
DarthJDG
  • 16,140
  • 11
  • 47
  • 55
user594775
  • 31
  • 1
1
dim path as string = FileUpload1.PostedFile.FileName

By the way, I am using Visual Studio 2010. I don't know if there is a difference with the other version.

J. Steen
  • 14,900
  • 15
  • 57
  • 62
GKL
  • 11
  • 1
1

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

1

I'm using IE 8 (on two separate machines). Each still uploads the full local file path. As suggested by Gabriël, Path.GetFileName(fileUploadControl.PostedFile.FileName) appears to be the only way to ensure that you only get the filename.

Greg
  • 193
  • 1
  • 12
1

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

user613465
  • 131
  • 1
  • 3
0

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.

Canavar
  • 46,286
  • 17
  • 83
  • 120
  • am developing an web application. I am using file upload control to browse files.It was working fine. Today I installed IE 8 After the installation I unable to get the full path of the file what could be the problem Event I uninstalled IE 8. Currently I am using IE 7. – Dhanraj Jul 15 '09 at 11:16
0
Path.GetFullPath(fileUpload.PostedFile.FileName);

Sorry this 'll get your program file directory + your file name.

Fahim Parkar
  • 28,922
  • 40
  • 153
  • 260
0

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.