Getting a file path using file upload control

I am using a file system control to display the contents of a text file in a text box. If i use this

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

string FilePath = txtBoxInput.PostedFile.FileName;

it will only get the file name e.g. bala.txt.i how to do itD:\New Folder\bala.txt

Instead of managing file uploads, I used a text box to get a path like this D:\New Folder\bala.txt

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

string FilePath = txtBoxInput.Text;

But I need a view button instead of a text field to get the path ... Any suggestion

EDIT: mouse click event

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();
    }
}
+3
source share
2 answers

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.
    }
}

, temp, , .

+7

Source: https://habr.com/ru/post/1776157/


All Articles