Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
829 views
in Technique[技术] by (71.8m points)

asp.net StreamReader - Timeouts are not supported on this stream

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...