使用Web API上传附件后阅读响应

问题描述 投票:1回答:1

我试图在使用web API c#上传附件后阅读回复。得到以下错误:

没有mediaTypeFormatter可用于从媒体类型为“multipart / form-data”的内容中读取“String”类型的对象。

这是我的代码部分:

string filepath =“C:/Users/O42895/Desktop/DownloadAttachmen20189t.jpg”; string filename =“DownloadAttachmen20189t.jpg”;

          MultipartFormDataContent content = new MultipartFormDataContent();
          ByteArrayContent fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(filepath));
          fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = filename };
          content.Add(fileContent);

          client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("multipart/form-data"));

          response = await client.PostAsync("https:--.com/flex/upload/document/genericattachment", content);

var resBody = response.Content.ReadAsStringAsync()。结果; Console.WriteLine(resBody);

我收到了回复但无法打印或阅读。如果有人知道,请告诉我

c# asp.net-web-api
1个回答
0
投票

似乎您需要在客户端的http标头请求中添加以下行(您将其余请求发送到您的web-api)。

希望这可以帮助。

Content-Type: application/json
© www.soinside.com 2019 - 2024. All rights reserved.