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条回答
无与为乐者.
2楼-- · 2019-01-01 06:23

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
查看更多
临风纵饮
3楼-- · 2019-01-01 06:24

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.

查看更多
临风纵饮
4楼-- · 2019-01-01 06:26

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

查看更多
何处买醉
5楼-- · 2019-01-01 06:27

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

查看更多
大哥的爱人
6楼-- · 2019-01-01 06:31
Convert.ToString(FileUpload1.PostedFile.FileName);
查看更多
时光乱了年华
7楼-- · 2019-01-01 06:31
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.

查看更多
登录 后发表回答