C# – Getting the path of a file using fileupload control

asp.netcfile upload

I am using a fileupload control to display the contents of a text file in a textbox..if i use this

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

string FilePath = txtBoxInput.PostedFile.FileName;

it will get only the file name like bala.txt.i need like this D:\New Folder\bala.txt

Instead of fileupload control i have used textbox to get the 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 browse button instead of textbox to get the path…Any Suggestion??

EDIT:My button 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();
    }
}

Best Answer

You can get the file name and path from FileUpload control only when you are in debug mode but when you deployed your app. in server then you cant because that is your client address which you are trying to access by server side code.

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

If you really want to change properties or rename client file then you can save file on server on temp folder then you can do all thing which you want.

Related Topic