当HttpRequest.Content.IsMimeMultipartContent()返回true时返回false [英] HttpRequest.Content.IsMimeMultipartContent() is returning false when it should return true

查看:2663
本文介绍了当HttpRequest.Content.IsMimeMultipartContent()返回true时返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一个HTTP请求作为MultiPartFormData发送到REST控制器。它工作正常,但现在我的控制器上的检查声称请求的类型不正确,即使我在调试器中看到请求的类型正确。供参考:

I need to send an HTTP request as a MultiPartFormData to a REST controller. It was working, but now the check I have on my controller is claiming that the request is not of the correct type, even when I can see in the debugger that the request is on the correct type. For reference:

这是控制台应用程序代码正在调用它:

Here's the console app code that is calling it:

using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;

namespace QuickUploadTestHarness
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var client = new HttpClient())
            using (var content = new MultipartFormDataContent())
            {
                // Make sure to change API address
                client.BaseAddress = new Uri("http://localhost");

                // Add first file content 
                var fileContent1 = new ByteArrayContent(File.ReadAllBytes(@"C:\<filepath>\test.txt"));
                fileContent1.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = "testData.txt"
                };

                //Add Second file content
                var fileContent2 = new ByteArrayContent(File.ReadAllBytes(@"C:\<filepath>\test.txt"));
                fileContent2.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = "Sample.txt"
                };

                content.Add(fileContent1);
                content.Add(fileContent2);

                // Make a call to Web API
                var result = client.PostAsync("/secret/endpoint/relevant/bits/here/", content).Result;

                Console.WriteLine(result.StatusCode);
                Console.ReadLine();
            }
        }
    }
}

如何是否有可能将其解释为不是MultiPartFormData?请注意使用MultiPartFormDataContent 作为请求

How is it possible that it's being interpreted as not MultiPartFormData? Note the "using MultiPartFormDataContent" for the request

推荐答案

对于 MultiPartFormDataContent 你可以尝试使用 content.Add 重载,它带有名称 filename 参数。
MSDN MultipartFormDataContent.Add Method(HttpContent,String,字符串)

For MultiPartFormDataContent you can try to use the content.Add overload that takes a name and filename argument. MSDN MultipartFormDataContent.Add Method (HttpContent, String, String)

问候

这篇关于当HttpRequest.Content.IsMimeMultipartContent()返回true时返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆