1

How do you read just part of a file as a stream in web api and do actions on this stream without taking the whole file in memory? NB: I don't want to save the file anywhere before reading - it's uploaded to a web api controller.

But what I really would like is the following pseudo code implemented:

foreach file in Request
{
    using (var sr = new StreamReader(fileStream))
    {
         string firstLine = sr.ReadLine() ?? "";
         if (firstLine contains the magic I need)
         {
             // would do something with this line, 
             // then scrap the stream and start reading the next file stream
             continue; 
         }
    }
}
Fred Johnson
  • 2,539
  • 3
  • 26
  • 52

1 Answers1

1

As found here: http://www.strathweb.com/2012/09/dealing-with-large-files-in-asp-net-web-api/

You can "Force Web API into streamed mode of dealing with the uploaded files, rather than buffering the entire request input stream in memory."

public class NoBufferPolicySelector : WebHostBufferPolicySelector
{
   public override bool UseBufferedInputStream(object hostContext)
   {
      var context = hostContext as HttpContextBase;

      if (context != null)
      {
         if (string.Equals(context.Request.RequestContext.RouteData.Values["controller"].ToString(), "uploading", StringComparison.InvariantCultureIgnoreCase))
            return false;
      }

      return true;
   }

   public override bool UseBufferedOutputStream(HttpResponseMessage response)
   {
      return base.UseBufferedOutputStream(response);
   }
}

public interface IHostBufferPolicySelector
{
   bool UseBufferedInputStream(object hostContext);
   bool UseBufferedOutputStream(HttpResponseMessage response);
}

Unfortunately it seems you can't get around it with Web API as mentioned in the post as this is heavily reliant on system.web.

Fred Johnson
  • 2,539
  • 3
  • 26
  • 52