Hi,

I'm trying to use the asp.net Input (file) with my program - i've placed it ont my program and I can click browse and it locates the file I want.

Eg: "C:\Users\Name\Desktop\test.rtf"

However, I then want to have it so that when I click a 'Submit' button it reads the contents of test.rtf.

How can I implement this into a button?

Thank you.

The FileUpload control has a .SaveAs(fileName) method. Save the file to the server, and then you can use a StreamReader (System.IO.StreamReader) object to read and work with the contents of the file.

I have the code working so that it locates the file however I receive an error once I click submit.

my code:

protected void Button2_Click(object sender, EventArgs e)
        {
                 
            // Read the file as one string.
            System.IO.StreamReader myFile =
            new System.IO.StreamReader(System.IO.Path.GetFullPath(File1.Value));

            string myString = myFile.ReadToEnd();

            myFile.Close();

            // Display the file contents.
            Console.WriteLine(myString);
            // Suspend the screen.
            Console.ReadLine();

            TextBox2.Text = myString;
        }

However the page says "Could not find file 'c:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\clioService.txt'."

However the file is located on my desktop??

>I have the code working so that it locates the file however I receive an error once I click submit.

It cannot work with asp.net (web-application). Look at apegram's post.

Ok thank you.

I have got a file upload working to C:\Test I can read the file i've uploaded by specifying its location.

How can I set it to read whatever file I have uploaded.

For example:

new System.IO.StreamReader(System.IO.Path.GetFullPath("C:\\Users\\Richard\\Desktop\\clioService.txt"));

- Rather than reading clioService.txt - how can I get it to read the FileUpload1 file that has been uploaded.

My code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FileUploadControl
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
                try
                {
                    FileUpload1.SaveAs("C:\\Test\\" +
                         FileUpload1.FileName);
                    Label1.Text = "File name: " +
                         FileUpload1.PostedFile.FileName + "<br>" +
                         FileUpload1.PostedFile.ContentLength + " kb<br>" +
                         "Content type: " +
                         FileUpload1.PostedFile.ContentType;
                }
                catch (Exception ex)
                {
                    Label1.Text = "ERROR: " + ex.Message.ToString();
                }
            else
            {
                Label1.Text = "You have not specified a file.";
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {

            System.IO.StreamReader myFile =
  new System.IO.StreamReader(System.IO.Path.GetFullPath("C:\\Users\\Richard\\Desktop\\clioService.txt"));

            string myString = myFile.ReadToEnd();

            myFile.Close();

            // Display the file contents.
            Console.WriteLine(myString);
            // Suspend the screen.
            Console.ReadLine();

            TextBox1.Text = myString;

        }
    }

}

You have two options,

1. FileUpload1.FileBytes - This property return the content of file in form of byte array.
2. Or use FileUpload1.PostedFile.InputStream - Return an instance of Stream.

Line #22 - To save a file

Use MapPath property of Server object to get absolute path,

FileUpload1.SaveAs(MapPath("~/" + FileUpload1.FileName));

Thank you kindly for your help.

I've used the following code to read the text from the file I have selected.

//Creating a new stream-reader and opening the file.
            System.IO.StreamReader StreamReader1 = new System.IO.StreamReader(FileUpload1.PostedFile.InputStream);

            //Reading Line from file
            String text = StreamReader1.ReadLine();

            //Writing the read line to textBox
            TextBox1.Text = text;

            //Close the file
            StreamReader1.Close();

Thanks for all the help this far its been really appreciated!

Why are you referring System.IO.Path.GetFullPath?

Have a look,
Method - 1

StreamReader sr = new StreamReader(FileUpload1.FileContent);
Response.Write(sr.ReadToEnd());

Method - 2

StreamReader sr = new StreamReader(FileUpload1.PostedFile.InputStream);
Response.Write(sr.ReadToEnd());
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.