Djama
Djama

Reputation: 791

Can't read file content when using others browsers excepts IE

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 ?

Upvotes: 1

Views: 448

Answers (1)

rene
rene

Reputation: 42434

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.

Upvotes: 1

Related Questions