0

Seems simple enough, but I don't see any tests in ServiceStack that focus on the uploaded aspect, everything seems to be focused on streaming the file to the browser. I'm not to concerned about the various clients that will use this, it's all in a controlled environment, so this technique looks interesting. Where might I find more information about uploading a large csv file in ServiceStack, and then consuming said file in my service i.e. getting the data to a database?

Sure enough, after digging a bit more I do see upload tests, but these seem to focus on saving the file to disk, is that the only option available, I was hoping to stream to memory and parse the file that way.

Thank you, Stephen

Community
  • 1
  • 1
Stephen Patten
  • 6,333
  • 10
  • 50
  • 84

1 Answers1

3

One approach is in the HttpBenchmarks example project for processing uploaded files which deserializes an Apache Benchmark into a typed POCO, which supports uploading multiple individual files as well as multiple files compressed in .zip files:

public object Post(UploadTestResults request)
{
    foreach (var httpFile in base.Request.Files)
    {
        if (httpFile.FileName.ToLower().EndsWith(".zip"))
        {
            // Expands .zip files
            using (var zip = ZipFile.Read(httpFile.InputStream))
            {
                var zipResults = new List<TestResult>();
                foreach (var zipEntry in zip)
                {
                    using (var ms = new MemoryStream())
                    {
                        zipEntry.Extract(ms);
                        var bytes = ms.ToArray();
                        zipResults.Add(new MemoryStream(bytes).ToTestResult());
                    }
                }
                newResults.AddRange(zipResults);
            }
        }
        else
        {
            // Converts
            var result = httpFile.InputStream.ToTestResult();
            newResults.Add(result);
        }
    }
    ...
}

In this example you would change ToTestResult() extension method to deserialize the CSV stream into a typed POCO.

ServiceStack itself doesn't have a built-in CSV deserializer, so you'll either need to find a .NET CSV Deserializer library or parse the uploaded CSV files manually.

mythz
  • 141,670
  • 29
  • 246
  • 390