简体   繁体   English

.NET HttpClient 图像上传到 PlantNet API

[英].NET HttpClient Image Upload to PlantNet API

I am trying to make a Request to the PlantNet API via .NET HttpClient.我正在尝试通过 .NET HttpClient 向 PlantNet API 发出请求。 I have a FileStream and I am using the StreamContent and when I look via debugger at the content before it is sent it's looking good.我有一个 FileStream,我正在使用 StreamContent,当我在发送之前通过调试器查看内容时,它看起来不错。 However PlantNet response is Unsupported file type for image[0] (jpeg or png) .但是 PlantNet 响应是Unsupported file type for image[0] (jpeg or png) I tried everything that came in my mind, the same request from VS Code Rest Client is working (with the same file), does anyone have any ideas if the StreamContent is messing somehow with the file data?我尝试了我想到的一切,来自 VS Code Rest Client 的相同请求正在工作(使用相同的文件),如果 StreamContent 以某种方式与文件数据混淆,是否有人有任何想法?

HttpResponseMessage responseMessage;
using (MultipartFormDataContent content = new("abcdef1234567890")) //Fixed boundary for debugging
{
  content.Add(new StringContent("flower"), "organs");

  using Stream memStream = new MemoryStream();
  await stream.CopyToAsync(memStream, cancellationToken);
  StreamContent fileContent = new(memStream);
  fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");

  content.Add(fileContent, "images", fileName);

  responseMessage = await _httpClient.PostAsync(url, content, cancellationToken);
}

Note: stream is the stream of the file, in this case it comes from an ASP.NET Core API controller using IFormFile.OpenReadStream() but I also tried opening the file directly via注意: stream是文件的流,在这种情况下,它来自使用IFormFile.OpenReadStream()的 ASP.NET Core API 控制器,但我也尝试通过直接打开文件

new FileStream("path", FileMode.Open, FileAccess.Read)

In the Debugger content.ReadAsStringAsync() resolves to the following在调试器content.ReadAsStringAsync()解析为以下

--abcdef1234567890
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=organs

flower
--abcdef1234567890
Content-Type: image/jpeg
Content-Disposition: form-data; name=images; filename=test-flower.jpeg; filename*=utf-8''test-flower.jpeg


--abcdef1234567890--

which is looking absolutely fine for me, so my guess is, that somehow the file binary data may be corrupt in the content or something?这对我来说看起来非常好,所以我的猜测是,文件二进制数据可能以某种方式在内容或其他内容中损坏? When I use the above for VS Code rest client with the same file it works and I get a successful response from the PlantNet API.当我将上述内容用于具有相同文件的 VS Code 休息客户端时,它可以工作,并且我从 PlantNet API 获得了成功的响应。

(Background: I am using .NET 6 on Fedora Linux) (背景:我在 Fedora Linux 上使用 .NET 6)

Ok I solved it by removing the copy to the memory stream.好的,我通过将副本删除到内存流来解决它。 This was needed as at first for debugging I opened the file directly and received exceptions if I didn't do it.这是需要的,因为一开始我直接打开文件进行调试,如果我不这样做,则会收到异常。 The code that is working for me is对我有用的代码是

HttpResponseMessage responseMessage;
using (MultipartFormDataContent content = new("abcdef1234567890"))
{
  content.Add(new StringContent("flower"), "organs");

  StreamContent fileContent = new(stream);
  fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
  fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
  {
    FileName = fileName,
    Name = "images"
  };

  content.Add(fileContent, "images", fileName);

  responseMessage = await _httpClient.PostAsync(url, content, cancellationToken);
}

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

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