简体   繁体   English

C#读取多部分流

[英]C# Reading multi-part stream

I'm reading a multi-part formdata in an HTTPHandler. 我正在HTTPHandler中读取多部分的formdata。

My code: 我的代码:

                var content = new StreamContent(context.Request.InputStream);

            //content.Headers.Add("Content-Type", context.Request.ContentType);
            //content.Headers.TryAddWithoutValidation("Content-Type", context.Request.ContentType);
            //content.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data");
            //content.Headers.Add("Content-Type", "multipart/form-data");
            //content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(context.Request.ContentType);

            // content.Headers.Remove("Content-Type");
            //content.Headers.Add("Content-Type", context.Request.ContentType);~

            content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse(context.Request.ContentType);
            if (!content.IsMimeMultipartContent())
            {
                return HttpStatusCode.BadRequest;
            }

            var multipart =await content.ReadAsMultipartAsync();

I always get: 我总是得到:

The format of value 'Content-Type: multipart/form-data; boundary=9fc46...................... is invalid

If I don't try to put the content-type I get another error. 如果我不尝试放置内容类型,则会出现另一个错误。

Note: The commented lines are other alternatives that I tried without result 注意:注释行是我尝试的其他选择,但没有结果

Working solution: 工作解决方案:

        var content = (HttpContent)new StreamContent(context.Request.InputStream);
        content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse(context.Request.ContentType);
        var multipart = new MultipartMemoryStreamProvider();
        await content.ReadAsMultipartAsync(multipart);

Try setting up a MultPartFormDataContent object with the following boundary and posting this 尝试使用以下边界设置MultPartFormDataContent对象并将其发布

var content = new MultipartFormDataContent("---------------" + Guid);

I believe the dashes have something to do with the way that boundarys are read in. 我相信破折号与读取边界的方式有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM