2

I have a problem reading uploaded XML file to the string instead of file.

My problem is that when I try to access the stream (var stream = part.ContentReadStream) then it's closed. I have feeling that it is accessing closed file stream. Am I using MultipartFormDataStreamProvider incorrectly? File size is only few kilobytes, so that should not be a problem.

    [WebInvoke(Method = "POST", UriTemplate = "{importFile}")]
    public HttpResponseMessage Upload(string importFile, HttpRequestMessage request)
    {
        if (!request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        // Create a stream provider for setting up output streams
        MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider();

        // Read the MIME multipart content using the stream provider we just created.
        var task = request.Content.ReadAsMultipartAsync(streamProvider);
        task.Wait();
        IEnumerable<HttpContent> bodyparts = task.Result;
        string submitter;
        if (!bodyparts.TryGetFormFieldValue("submitter", out submitter))
        {
            submitter = "unknown";
        }

        // Get list of local file names from stream provider
        IDictionary<string, string> bodyPartFileNames = streamProvider.BodyPartFileNames;


        var parser = this.parserFactoryFactory.CreateParser();
        foreach (var part in bodyparts)
        {
            using (var stream = part.ContentReadStream)
            {
                using (var streamReader = new StreamReader(stream))
                {
                    string content = streamReader.ReadToEnd();
                    var results = parser.Parse(content);
                }
            }
        }
        return new HttpResponseMessage(HttpStatusCode.Accepted);
    }  

This is my post

<h3>Data import test</h3>

<form action="/api/data/Upload" enctype="multipart/form-data" method="POST">
    <input type="file" name="importFile"/>
    <input type="submit" value="Upload"/>
</form>
Tx3
  • 6,796
  • 4
  • 37
  • 52

1 Answers1

4

The solution was actually quite simple. MultipartFormDataStreamProvider is not required when we are not dealing with files. This works quite smoothly on my case.

[WebInvoke(Method = "POST", UriTemplate = "{importFile}")]
public HttpResponseMessage Upload(
    string importFile, HttpRequestMessage request)
{
    if (!request.Content.IsMimeMultipartContent())
        throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);

    // Read the MIME multipart content 
    var task = request.Content.ReadAsMultipartAsync();            
    task.Wait();

    IEnumerable<HttpContent> bodyparts = task.Result;
    string submitter;
    if (!bodyparts.TryGetFormFieldValue("submitter", out submitter))
        submitter = "unknown";

    var parser = this.parserFactoryFactory.CreateParser();
    foreach (var part in bodyparts)
    {
        using (var stream = part.ContentReadStream)
        {
            using (var streamReader = new StreamReader(stream))
            {
                string content = streamReader.ReadToEnd();
                var results = parser.Parse(content);
                if (results.IsValid)
                    // do something
            }
        }
    }
    var message = new HttpResponseMessage(HttpStatusCode.Accepted);
    return message;
}  
Tx3
  • 6,796
  • 4
  • 37
  • 52
  • 4
    Does this even compile? task.Result is a MultipartMemoryStreamProvider - how does that cast to IEnumerable ? I think it should be task.Result.Contents – Chris Rogers Sep 04 '13 at 23:05
  • It's been a while since question was answered. Unfortunately at the moment I can't verify is this still valid solution. – Tx3 Sep 05 '13 at 08:46
  • What is `this.parserFactoryFactory.CreateParser();` doing? – StuperUser Jul 03 '15 at 08:14
  • @StuperUser I have no idea, it's been four years when I answered the question – Tx3 Sep 19 '15 at 11:57