31

I have an ASP.Net Web API application that allows clients (html pages and iPhone apps) to upload images to. I am using an async upload task as described in this article.

Everything works great when I want to save to the file system because that's what this code does automatically, behind the scenes it seems. But, I don't want to save the uploaded files to the file system. Instead, I want to take the uploaded stream and pass it through to an Amazon S3 bucket using the AWS SDK for .Net.

I have the code set up to send the stream up to AWS. The problem I can't figure out is how to get the uploaded content stream from the Web API method instead of having it automatically save to disk.

I was hoping there would be a virtual method I could override in MultipartFormDataStreamProvider which would allow me to do something else with the uploaded content other than save to disk, but there doesn't seem to be.

Any suggestions?

Dmitry Pavliv
  • 35,333
  • 13
  • 79
  • 80
Stoop
  • 1,235
  • 3
  • 17
  • 23
  • Hi, I know it's an old question, but it's helped me a lot. By any chance can you post the complete code for creating the AWS stream too? I can't figure out how to return the AWS stream in the accepted answer below, or what to do with it afterwards. – l3utterfly Mar 24 '16 at 00:53

2 Answers2

45

You could override MultipartFormDataStreamProvider's GetStream method to return a stream which is not a file stream but your AWS stream, but there are some issues doing so(which I will not elaborate here). Instead you could create a provider deriving from the abstract base class MultipartStreamProvider. Following sample is heavily based on the actual source code of MultipartFormDataStreamProvider and MultipartFileStreamProvider. You can check here and here for more details. Sample below:

public class CustomMultipartFormDataStreamProvider : MultipartStreamProvider
{
    private NameValueCollection _formData = new NameValueCollection(StringComparer.OrdinalIgnoreCase);

    private Collection<bool> _isFormData = new Collection<bool>();

    private Collection<MyMultipartFileData> _fileData = new Collection<MyMultipartFileData>();

    public NameValueCollection FormData
    {
        get { return _formData; }
    }

    public Collection<MultipartFileData> FileData
    {
        get { return _fileData; }
    }

    public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
    {
        // For form data, Content-Disposition header is a requirement
        ContentDispositionHeaderValue contentDisposition = headers.ContentDisposition;
        if (contentDisposition != null)
        {
            // If we have a file name then write contents out to AWS stream. Otherwise just write to MemoryStream
            if (!String.IsNullOrEmpty(contentDisposition.FileName))
            {
                // We won't post process files as form data
                _isFormData.Add(false);

                 MyMultipartFileData fileData = new MyMultipartFileData(headers, your-aws-filelocation-url-maybe);
                 _fileData.Add(fileData);

                return myAWSStream;//**return you AWS stream here**
            }

            // We will post process this as form data
            _isFormData.Add(true);

            // If no filename parameter was found in the Content-Disposition header then return a memory stream.
            return new MemoryStream();
        }

        throw new InvalidOperationException("Did not find required 'Content-Disposition' header field in MIME multipart body part..");
    }

    /// <summary>
    /// Read the non-file contents as form data.
    /// </summary>
    /// <returns></returns>
    public override async Task ExecutePostProcessingAsync()
    {
        // Find instances of HttpContent for which we created a memory stream and read them asynchronously
        // to get the string content and then add that as form data
        for (int index = 0; index < Contents.Count; index++)
        {
            if (_isFormData[index])
            {
                HttpContent formContent = Contents[index];
                // Extract name from Content-Disposition header. We know from earlier that the header is present.
                ContentDispositionHeaderValue contentDisposition = formContent.Headers.ContentDisposition;
                string formFieldName = UnquoteToken(contentDisposition.Name) ?? String.Empty;

                // Read the contents as string data and add to form data
                string formFieldValue = await formContent.ReadAsStringAsync();
                FormData.Add(formFieldName, formFieldValue);
            }
        }
    }

    /// <summary>
    /// Remove bounding quotes on a token if present
    /// </summary>
    /// <param name="token">Token to unquote.</param>
    /// <returns>Unquoted token.</returns>
    private static string UnquoteToken(string token)
    {
        if (String.IsNullOrWhiteSpace(token))
        {
            return token;
        }

        if (token.StartsWith("\"", StringComparison.Ordinal) && token.EndsWith("\"", StringComparison.Ordinal) && token.Length > 1)
        {
            return token.Substring(1, token.Length - 2);
        }

        return token;
    }
}

public class MyMultipartFileData
{
    public MultipartFileData(HttpContentHeaders headers, string awsFileUrl)
    {
        Headers = headers;
        AwsFileUrl = awsFileUrl;
    }

    public HttpContentHeaders Headers { get; private set; }

    public string AwsFileUrl { get; private set; }
}
Kiran
  • 56,921
  • 15
  • 176
  • 161
  • 1
    Brilliant! You've helped me understand things immensely. First of all, I now see "how" the files are actually getting saved to disk (this was previously unknown to me). It is happening in GetStream of MultipartFileStreamProvider (which is base of MultipartFormDataStreamProvider, from which my custom provider is deriving). Now, what I can do is derive directly from MultipartStreamProvider and override GetStream so that it does not save to disk but rather commits to S3, as you have suggested. Many thanks! – Stoop Apr 05 '13 at 21:20
  • 5
    I would really like to see the part clarified where the stream gets written to the myAWSStream. I would like to write something to an Azure Blob storage instead of AWS. But I don't know how (in method GetStream) to get access to the stream. In this method I would like to do: BlobService.StoreImageToBlobFromStream(stream) – woutercx Nov 02 '13 at 23:31
  • 3
    That the solution appears to be "let's create our own class by copying a load of code from this class and the rest from this class" almost makes me cry. – mwardm Jan 17 '14 at 21:19
  • 5
    Yeah it is unexplainable that MS would ship an API that *requires* access to the filesystem to do processing of such a basic scenario. It'd like they couldn't think that people might not to want to have access to file writing and perform disk IO to handle a form POST. Grr. – MichaelGG Jun 13 '14 at 00:21
  • 1
    @Kiran can you please elaborate on the full solution this is unusable to me as I don't understand where you got myAWSStream var. – parliament Feb 16 '15 at 17:46
  • By returning the stream reference on GetStream, the file stream is written to the stream location directly. If you need to define a container as in the case of a blob system, we need to define that information when instantiating the stream reference. The container information can be resolved using a header value because if another input parameter is sent, it is only available on a second call to GetStream.The header needs to be taken from the Request obj. This can be handled by adding a property to the CustomMultipart.. which can be set by the controller. – ozkary Dec 31 '15 at 19:46
  • @Kiran - I'm struggling with creating a solution simliar to this but streams to the chunks to SQL instead. My issue is that when I return my custom stream to handle the writes, it is including the request headers as well. So when I send the chunks back to the user, they create an invalid file because the headers are included in the content. Is there a way to filter out the headers when writing the content back? – JakeHova Nov 18 '16 at 21:20
  • @woutercx - I just found [this blog](http://www.codeguru.com/csharp/.net/uploading-files-asynchronously-using-asp.net-web-api.htm), if you're still looking for a Azure-solution. – smoksnes Jul 14 '17 at 06:18
  • 1
    Would you be so kind as to elaborate on the issues when overriding `MultipartFormDataStreamProvider.GetStream()`? – user247702 Sep 27 '17 at 12:13
4

Since @KiranChalla posted their answer, a new abstract class MultipartFormDataRemoteStreamProvider was introduced in Fix 1760: Make MultipartFormDataStreamProvider easier to work with non-FileStreams. to make this easier.

The summary of the class does a good job at explaining how to use it:

A MultipartStreamProvider implementation suited for use with HTML file uploads for writing file content to a remote storage Stream. The stream provider looks at the Content-Disposition header field and determines an output remote Stream based on the presence of a filename parameter. If a filename parameter is present in the Content-Disposition header field, then the body part is written to a remote Stream provided by GetRemoteStream. Otherwise it is written to a MemoryStream.

user247702
  • 23,641
  • 15
  • 110
  • 157