1

I am trying to a pass a stream from a HttpPostedFileBase to a service method without saving to disk.

//Controller Action
[HttpPost]
    public void Import()
    {
        if (Request.Files.Count == 0)
            return new HttpStatusCodeResult(404);

        var file = Request.Files[0];

        _departmentAppService.Import(file.InputStream);
    }

//Service Method
public string Import(Stream stream)
    {
        var excelFile = ExcelFile.Load(stream, LoadOptions.CsvDefault);
        var worksheet = excelFile.Worksheets[0];
        var jsonString = worksheet.ToJsonString();
        return jsonString;
    }

An error of "Property accessor 'ReadTimeout' on object 'System.Web.HttpInputStream' threw the following exception:'Timeouts are not supported on this stream.'" is thrown when the stream is passed to the method. Is there an idiomatic method of doing this?

bjscharf
  • 61
  • 10

1 Answers1

1

I was not able to reproduce this issue so I cannot inspect the stack trace that you get, but nevertheless I presume you get timed out because you're processing large CSV file in which case I would try to increase the 'executionTimeout' in web.config.

Also you could try using a temporary memory stream to store an uploaded file, something like the following:

var file = Request.Files[0];
using (var tempStream = new MemoryStream())
{
    file.InputStream.CopyTo(tempStream);
    tempStream.Position = 0;
    _departmentAppService.Import(tempStream);
}

I hope this helps.

Mario Z
  • 4,328
  • 2
  • 24
  • 38
  • Thank you for your reply. I still get the same error: Property accessor 'ReadTimeout' on object 'System.IO.MemoryStream' threw the following exception:'Timeouts are not supported on this stream.' The stack trace is: at System.IO.Stream.get_ReadTimeout() The file is 1 KB Do you have any other suggestions? – bjscharf Aug 31 '16 at 12:53
  • Unfortunately no, but would you be able to temporarely upload somewhere a VS test project that would reproduce your issue so that I may investigate it? – Mario Z Aug 31 '16 at 13:05
  • 2
    Thank you for your help. After many hours of testing it appears that this is related to the Asp.Net Boiler Plate framework we are using. When we updated to the latest version (https://github.com/aspnetboilerplate/aspnetboilerplate/releases/tag/v0.11.2.0) and changed a lot of code to match, then this error seems to go away. I very much appreciate your help. – bjscharf Sep 06 '16 at 13:48