tags:

views:

83

answers:

1

I have a web page on which users can upload text files (but a text file, i.e. a file with the extension .txt, could be of many encodings, e.g. ASCII, UTF8, UNICODE .. etc), I'm trying to validate the contents in memory before I save the file to the disk, if the content is not valid, I don't save the file. I'm reading the content from the file upload control (fileUpload1.FileContent which returns a stream of bytes), is there an easy way in .NET to convert the content of the uploaded file to a string (i.e. the byte stream returned from fileUpload1.FileContent) or will I have to check the first bytes to detect the encoding first?

Thanks

+4  A: 

I think you can do this:

StreamReader reader = new StreamReader(fileUpload1.FileContent);
string text = reader.ReadToEnd();
Alek Davis
That worked very well, thanks a lot
Waleed Eissa