Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HttpClient后期上载失败,未传输任何文件,也未生成任何格式的ContentDisposition_C#_Api_Http_.net Core_Dotnet Httpclient - Fatal编程技术网

C# HttpClient后期上载失败,未传输任何文件,也未生成任何格式的ContentDisposition

C# HttpClient后期上载失败,未传输任何文件,也未生成任何格式的ContentDisposition,c#,api,http,.net-core,dotnet-httpclient,C#,Api,Http,.net Core,Dotnet Httpclient,我尝试在自己的映像宿主上镜像映像,该宿主包含一个简单的API,可接受如下默认表单数据上载: -----------------------------149841124823007 Content-Disposition: form-data; name="file"; filename="ZMVdEwM.png" Content-Type: image/png <Binary image data...> 我正在下载测试图像作为字节数组,并

我尝试在自己的映像宿主上镜像映像,该宿主包含一个简单的API,可接受如下默认表单数据上载:

-----------------------------149841124823007
Content-Disposition: form-data; name="file"; filename="ZMVdEwM.png"
Content-Type: image/png
<Binary image data...>
我正在下载测试图像作为字节数组,并尝试构建相同的表单数据上传。但它在我的api上失败了:

if (!isset($_FILES['file'])) {
    echo json_encode(array('errorcode' => 'no_image_transfered'));
}
在调查问题时,我检查了名为
content
MultipartFormDataContent
实例,因为它负责构建请求主体。它两次显示包含文件名的
ContentDisposition
属性:第一个是正确的,但第二个看起来是错误的:


我的POST请求有什么问题?

发现
MultipartFormDataContent.Headers.ContentType
是HTTP头的值:

Content-Type: multipart/form-data; boundary=--------------149841124823007
这是取自的,它破坏了我的API,因为它需要
多部分/表单数据
。因此,最好关闭该类型,除非您的API在
$\u FILES['file'][0]['type']
中检查提供的文件类型,因为该类型为空。类型来自主体:

Content-Disposition: form-data; name="file"; filename="ZMVdEwM.png"
Content-Type: image/png <--- type
否则,如果您对不包含mime类型的
multipart/form data
upload很满意,请按如下方式进行上传:

content.ElementAt(0).Headers.ContentType = MediaTypeHeaderValue.Parse("image/png");
string url = "https://i.imgur.com/0acC9nr.png";
var client = new HttpClient();
var imageData = client.GetByteArrayAsync(url).Result;

var content = new MultipartFormDataContent();
string fileName = new Uri(url).LocalPath.Replace("/", "");
content.Add(new ByteArrayContent(imageData), "file", fileName);
// Optionally when the Content-Type body field is required
// content.ElementAt(0).Headers.ContentType = MediaTypeHeaderValue.Parse("image/png");
var postResp = client.PostAsync("https://my-image-hoster/api.php", content).Result;
var resp = postResp.Content.ReadAsStringAsync().Result;
string url = "https://i.imgur.com/0acC9nr.png";
var client = new HttpClient();
var imageData = client.GetByteArrayAsync(url).Result;

var content = new MultipartFormDataContent();
string fileName = new Uri(url).LocalPath.Replace("/", "");
content.Add(new ByteArrayContent(imageData), "file", fileName);
// Optionally when the Content-Type body field is required
// content.ElementAt(0).Headers.ContentType = MediaTypeHeaderValue.Parse("image/png");
var postResp = client.PostAsync("https://my-image-hoster/api.php", content).Result;
var resp = postResp.Content.ReadAsStringAsync().Result;