-1

i'm fetching a file by HttpRequestedFileBase, txt type, and I need to read each line in the file and store those line in variables to workin with them. But I'm wondering if i should convert the file to read it. Also I get some errors whiles I was trying to implement the read.

var file = Request.Files[0];
string value = Request.Form["objectList"].ToString();
string[] keys = Request.Form.AllKeys;
string banc = form["objectlistbanc"].ToString();

var lix = file.InputStream; //not sure if inputsteam is what i need

var lines = File.ReadAllLines(lix); // error here says i need a fileContenResult using file content and file type
foreach (var line in lines) {
  string ln = line;
}
Matan Shahar
  • 3,190
  • 2
  • 20
  • 45

1 Answers1

1

Yes, following MSDN, you need to convert the stream to read it.

But you can simply call this method to read the content of the file.

 string content = new StreamReader(Request.Files[0].InputStream).ReadToEnd();

or to read line by line

using (var reader = new StreamReader(Request.Files[0].InputStream)) 
{ 
   while (!reader.EndOfStream) 
   { 
      var line = reader.ReadLine(); 
   } 
}
Antoine V
  • 6,998
  • 2
  • 11
  • 34
  • ok so I tried this with a for each but it's reading every single caracter from the row and i would like to catch a row for each read, is there any way to do something like that – E.Rawrdríguez.Ophanim Jul 16 '18 at 17:31
  • using (var reader = new StreamReader(content)) { while (!reader.EndOfStream) { var line = reader.ReadLine(); } } – Antoine V Jul 16 '18 at 18:16
  • Error says 'invalid' characters D: – E.Rawrdríguez.Ophanim Jul 16 '18 at 18:45
  • > System.ArgumentException: invalid characters in the "path". > string content = new StreamReader(file.InputStream).ReadToEnd(); using (var reader = new StreamReader(content)) { while (!reader.EndOfStream) { var line = reader.ReadLine(); Extraer(line); } } – E.Rawrdríguez.Ophanim Jul 16 '18 at 20:10
  • But need those caracters in my file name it's a ' ' blanck space what's getting me in trouble – E.Rawrdríguez.Ophanim Jul 16 '18 at 20:18
  • can't answer, it convention .net – Antoine V Jul 16 '18 at 20:36
  • @E.Rawrdríguez.Ophanim finally, you resolve your problem?. Don't forget to mark the answer if my answer solves your problem. – Antoine V Jul 17 '18 at 07:29
  • well, yes I solved but I'm not using your method, at the end I didi this: > string content = new StreamReader(file.InputStream).ReadToEnd(); for (int i = 0; i < content.Length; i++) { var a = content.Substring(0, content.IndexOf("\n", i)); Extraer(a); content = content.Remove(0, content.IndexOf("\n", i)); } would you give me any advice? – E.Rawrdríguez.Ophanim Jul 17 '18 at 13:49