I am relatively new to .net and I am uncertain why I am having issues with a StreamReader. Ultimately the project receives a CSV file which I write to disk and then try to parse using CsvReader. It writes to disk as expected and the absolute path passed to the parser is valid. Using the sample code from CsvReader returned a null object and its explained by the reader returning an error "Timeouts are not supported on this stream".
My index calls the controller using ajax
function uploadFile(file) {
var _url = '@Url.Action("index", "FileUpload")';
var formData = new FormData();
formData.append("file", file);
$.ajax({
url: _url,
data: formData,
processData: false,
contentType: false,
type: "POST",
success: function (data) {
$('.fileStatus').text("File Uploaded");
showFile(file);
},
error: function (jqXHR) {
$('.fileStatus').text("File Error");
}
});
}
FileUploadController writes this file out to disk and then passes the absolute path over to the CSV parser method for it to read it in.
[HttpPost("FileUpload")]
public async Task<IActionResult> Index(IFormFile file)
{
if (file != null)
{
var filename = this.EnsureCorrectFilename(file.FileName);
var csvFile = this.GetPathAndFilename(filename);
using (var stream = new FileStream(csvFile, FileMode.Create))
{
file.CopyTo(stream);
}
var csv = csvController.ReadInCSV(csvFile);
}
return Ok();
}
the reader here is erroring. I am uncertain why 'reader.BaseStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException'. Message "Timeouts are not supported on this stream."
public IEnumerable<dynamic> ReadInCSV(string absolutePath)
{
using (var reader = new StreamReader(absolutePath))
using (var csvReader = new CsvReader(reader, CultureInfo.InvariantCulture))
{
csvReader.Configuration.RegisterClassMap<InvoiceMap>();
var records = csvReader.GetRecords<InvoiceModel>().ToArray();
return records;
}
}
question from:
https://stackoverflow.com/questions/65836631/asp-net-streamreader-timeouts-are-not-supported-on-this-stream 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…