Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File Streaming upload via Web API to a StreamReader

Basically I want to stream a file to a web API and once inside the web api controller I would like to pass data as it comes in to lower level logic via a stream reader. I tried the code below found from another SO post with some modification, but I get:

An asynchronous module or handler completed while an asynchronous operation was still pending.

    public async void Put(int id, HttpRequestMessage request)
    {
        if (!Request.Content.IsMimeMultipartContent())
            throw new InvalidOperationException();

        var provider = new MultipartMemoryStreamProvider();
        await Request.Content.ReadAsMultipartAsync(provider);

        var file = provider.Contents.First();
        var filename = file.Headers.ContentDisposition.FileName.Trim('\"');
        var buffer = await file.ReadAsByteArrayAsync();
        var stream = new MemoryStream(buffer);

        using (var s = new StreamReader(stream))
        {
            saveFile.Execute(id, s);
        }
    }

I'm open to other solutions as long as I am streaming the data as it comes in. I'm new to await and async and I'm probably making a basic mistake. Any ideas?

like image 232
Joshua Enfield Avatar asked May 02 '14 21:05

Joshua Enfield


1 Answers

Change async void to async Task

like image 166
Kiran Avatar answered Sep 22 '22 21:09

Kiran