IFileListEntry.Data stream causes problems with using StreamReader
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 Try using the file.Data.CopyToAsync(stream) first then using the CsvReader.
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 ?
`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