1

When i try this code using IE i can read the content of the file because IE gives the full path oh the file.

       void SaveBranchDetails()
        {
           string fileLocation = AsyncFileUploadBranch.PostedFile.FileName;
           try
           {
             BranchData b = new BranchData();
             StreamReader sr = new StreamReader(fileLocation);
             var result = CSV.ReadingCSVFile<BranchData>(sr);
              foreach (var rec in result)
              {
                //get details
                  b.Id = rec.Id;
                  b.Branch = rec.Branch;
                  b.Active = rec.Active;
                  b.SaveBranches();
              }
              sr.Close();
        }
        catch (Exception)
        {
            throw;
        }

    }

Using the others browsers to perform the same task give me an error:

  Could not find file 'C:\Program Files (x86)\IIS Express\branches.csv'.

At the following line :

 StreamReader sr = new StreamReader(fileLocation);

How can i fix this problem ?

Djama
  • 661
  • 5
  • 11
  • 28
  • 1
    what is `AsyncFileUploadBranch`? where is this code running? on client? on server? where does the browser fit in this code? i cant see any reference to IE. – inquisitive Apr 03 '14 at 09:43
  • @inquisitive AsyncFileUploadBranch is the name of the AsyncFileUpload. when i upload the file using IE i can read the content of the file but using another browser i can't read the content and i posted the error and the line that causes it. – Djama Apr 03 '14 at 09:52

1 Answers1

1

You need to use the FileContent property because that is the Stream of bytes that is returned from the browser.

If you change your code as follows I expect it to work for all browsers:

 BranchData b = new BranchData();
 using(var sr = new StreamReader(AsyncFileUploadBranch.FileContent))
 {
     using (var csvReader = new CsvReader(sr))
     {
         var result = csvReader.GetRecords<BranchData>().ToArray();
         foreach (var rec in result)
         {
            //get details
            b.Id = rec.Id;
            b.Branch = rec.Branch;
            b.Active = rec.Active;
            b.SaveBranches();
         }
     }
 }

If that doesn't work you might also try AsyncFileUploadBranch.PostedFile.InputStream instead.

rene
  • 41,474
  • 78
  • 114
  • 152
  • `CSV.ReadingCSVFile(streamreader sr);` uses streamreader as input not inputstream or file content. @rene – Djama Apr 03 '14 at 12:47
  • i tried it but having this error `You must call read on the reader before accessing its data.` at the following lines `var result = CSV.ReadingCSVFile(sr);` – Djama Apr 03 '14 at 12:56
  • i need to read the data from a csv file with the following data (id, branch,active). BranchData is a class that has the proprieties and method to save the read data from the csv file. – Djama Apr 03 '14 at 13:03
  • i solved the problem by looking at this link: http://stackoverflow.com/questions/11086942/using-csvhelper-on-file-upload – Djama Apr 03 '14 at 13:35