Reading content to the memory from the multi-part file upload

南楼画角 提交于 2019-12-06 04:19:14

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;
}  
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!