How to read file contents using Fileupload

Is it possible to read the contents of a file using Fileupload.

For example, I want to save an XML file in a database, the user searches for the file using Fileupload and then click the button to save the contents of the file in the database.

I tried this one but it doesn't work

string s=Fileuploder1.Filecontent.tostring();

but without success, do you have any ideas?

+5
source share
2 answers
string inputContent;
using (StreamReader inputStreamReader = new StreamReader(InputFileUpload.PostedFile.InputStream))
{
    inputContent = inputStreamReader.ReadToEnd();
}
+17
source

we cannot read the file directly, instead we must save it at the project location. using the path to the project file, we can read using the stream reader.

var filePath = Path.Combine(Server.MapPath("~/Document"), fileName);
                file.SaveAs(filePath);

                if (!string.IsNullOrEmpty(filePath))
                {
                    using (StreamReader sr = new StreamReader(Path.Combine(Server.MapPath("~/Document"), fileName)))
                    {
                        while (sr.Peek() >= 0)
                        {
                            strbuild.AppendFormat(sr.ReadLine());
                        }
                    }

                }

for more details: http://www.infinetsoft.com/Post/How-to-read-text-file-using-fileupload-control-in-asp-net-MVC/1245

-1

All Articles