BlazorInputFile icon indicating copy to clipboard operation
BlazorInputFile copied to clipboard

IFileListEntry.Data stream causes problems with using StreamReader

Open MylesRip opened this issue 6 years ago • 3 comments

A StreamReader that is constructed by passing in IFileListEntry.Data as a parameter appears to be incompatible with CsvHelper. Any attempt to work with a CsvReader created using the StreamReader receives the following error when you try to use it: "Synchronous reads are not supported"

Example:

IFileListEntry file = files.FirstOrDefault();

using (var reader = new System.IO.StreamReader(file.Data))
using (var csv = new CsvReader(reader))
{
  using (var dr = new CsvDataReader(csv)) // error here: "synchronous reads are not supported"
  {
    var dt = new DataTable();
    dt.Load(dr);
  }
}

Since streams from other sources, such as reading a file into a FileStream, seem to work fine with CsvHelper, I suspect the issue may lie with something unique to the implementation of IFileListEntry.Data.

MylesRip avatar Nov 13 '19 18:11 MylesRip

@MylesRip Try using the file.Data.CopyToAsync(stream) first then using the CsvReader.

periapsistech avatar Nov 16 '19 23:11 periapsistech

i have same issue i want to save the file. any help other then sending request to webapi. i should be able to save file with in blazor ?

aqkhana2002 avatar Apr 06 '20 05:04 aqkhana2002

`file = files.FirstOrDefault();

    var oPath = System.IO.Directory.GetCurrentDirectory();
    var oCompletePath = oPath + "/Plans/Plan_" + PageFileDocumentModel.PlanID.ToString().Trim();
    var oExtfrom = file.Name.IndexOf(".");
    var oFileName = file.Name.ToString().Substring(0, oExtfrom) + "_" + DateTime.Now.ToString("dd_MM_yyyy_hh_mm") + file.Name.Substring(oExtfrom, file.Name.Length - oExtfrom);
    if (!(System.IO.Directory.Exists(oCompletePath)))
    {
        System.IO.Directory.CreateDirectory(oCompletePath);
    }

    var ms = new System.IO.MemoryStream();
    await file.Data.CopyToAsync(ms);

    status = $"Finished loading {file.Size} bytes from {file.Name}";


    using (System.IO.FileStream Source = new System.IO.FileStream(oCompletePath + "/" + oFileName, System.IO.FileMode.Create, System.IO.FileAccess.Write))
    {
        ms.WriteTo(Source);
    }

`

solved using this code

aqkhana2002 avatar Apr 06 '20 05:04 aqkhana2002