使用fileupload控件获取文件的路径

bal*_*569 3 c# asp.net file-upload

我正在使用fileupload控件在文本框中显示文本文件的内容..如果我使用它

<asp:FileUpload ID="txtBoxInput" runat="server" Text="Browse" />

string FilePath = txtBoxInput.PostedFile.FileName;
Run Code Online (Sandbox Code Playgroud)

它只会得到像bala.txt.i这样的文件名D:\New Folder\bala.txt

而不是fileupload控件我已经使用文本框来获取这样的路径 D:\New Folder\bala.txt

<asp:TextBox ID="txtBoxInput" runat="server" Width="451px"></asp:TextBox>

string FilePath = txtBoxInput.Text;
Run Code Online (Sandbox Code Playgroud)

但我需要浏览按钮而不是文本框来获取路径......任何建议?

编辑:我的按钮单击事件

protected void buttonDisplay_Click(object sender, EventArgs e)
{
    string FilePath = txtBoxInput.PostedFile.FileName;
    if (File.Exists(FilePath))
    {
        StreamReader testTxt = new StreamReader(FilePath);
        string allRead = testTxt.ReadToEnd();
        testTxt.Close();
    }
}
Run Code Online (Sandbox Code Playgroud)

Jee*_*att 7

只有在处于调试模式但部署应用程序时,才能从FileUpload控件获取文件名和路径.在服务器然后你不能,因为这是你试图通过服务器端代码访问的客户端地址.

protected void Button1_Click(object sender, EventArgs e)
{
    string filePath,fileName;
    if (FileUpload1.PostedFile != null)
    {
        filePath = FileUpload1.PostedFile.FileName; // file name with path.
        fileName = FileUpload1.FileName;// Only file name.
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您真的想要更改属性或重命名客户端文件,那么您可以将文件保存在临时文件夹的服务器上,然后您可以执行所需的所有操作.