0

Hi,

I'm trying to get my program to write the content of textbox1 into a textfile specified at a particular location. However I get an error when trying to do so.

My code:

protected void btnSave_Click(object sender, EventArgs e)
        {
           
            //Write to fileupload location
            System.IO.StreamWriter StreamWriter1 = new System.IO.StreamWriter(FileUpload1.PostedFile.FileName);

            //Writing text to the file.
           StreamWriter1.Write(TextBox1.Text);

            //Close the file.
           StreamWriter1.Close();

        }

Now i've checked the FileUpload1.Postfiled.Filename and it shows as text.txt - rather than the location clearly entered into FileUpload1 which is "C:\\Users\\Name\\Desktop\\test.txt".

If i specify the location it works perfectly, however I want user to first select the file and then save.

Thank you kindly.

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by __avd
0

>I'm trying to get my program to write the content of textbox1 into a textfile specified at a particular location.

You can not save file outside your web application. In fact , web app can not perform read / write action on client machine.

I think you misunderstood FileUpload control. FileUpload control is used to read user input in form of byte array(binary data).

However, you may create/open data file in the context of your web application.

protected void btnSave_Click(object sender, EventArgs e)
        {
           
            //Write to fileupload location
            System.IO.StreamWriter StreamWriter1 = new System.IO.StreamWriter(MapPath("~/file.txt"));

            //Writing text to the file.
           StreamWriter1.Write(TextBox1.Text);

            //Close the file.
           StreamWriter1.Close();

        }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.