What is the shortest way to get the string content of a HttpPostedFile in C#
Asked Answered
A

1

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.)

Allstar answered 27/4, 2012 at 3:44 Comment(1)
+1, especially for following up with the actual problem/ solution.Handmade
G
45
var str = new StreamReader(postedFile.InputStream).ReadToEnd();

StreamReader.ReadToEnd

Gloucester answered 27/4, 2012 at 3:49 Comment(1)
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 @AlexeiLevenkovAllstar

© 2022 - 2024 — McMap. All rights reserved.