22

I have an HttpPostedFile object and just need to extract the content of the posted file.

I found this link but that's a very long process to just obtain a string with the content.

Is there any shorter way? (Preferably, a one line instruction.)

Alex Angas
  • 59,219
  • 41
  • 137
  • 210
yorch
  • 7,170
  • 8
  • 32
  • 38

1 Answers1

45
var str = new StreamReader(postedFile.InputStream).ReadToEnd();

StreamReader.ReadToEnd

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
  • 4
    Thanks, that worked flawless. I was trying that before making the question but I was getting an empty string so wasn't sure that was the correct way.The problem was that I needed to set the position of the reader back to 0 (`postedFile.InputStream.Position = 0`) as this was the second time on the code reading the content. Thanks again @LenielMacaferi and @AlexeiLevenkov – yorch Apr 27 '12 at 15:05