【问题标题】:Read response after uploading attachment using Web API使用 Web API 上传附件后读取响应
【发布时间】:2019-01-27 20:35:01
【问题描述】:

我正在尝试使用 Web API c# 上传附件后读取响应。出现以下错误:

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

这是我的部分代码:

字符串文件路径 = "C:/Users/O42895/Desktop/DownloadAttachmen20189t.jpg"; 字符串文件名 = "下载附件20189t.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().Result; Console.WriteLine(resBody);

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

【问题讨论】:

  • 为什么是await client.PostAsync,然后是ReadAsStringAsync().Result?为什么不await 两者兼而有之?
  • 你不需要:client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("multipart/form-data"));
  • 我已删除该行,但仍无法正常工作。

标签: c# asp.net-web-api


【解决方案1】:

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

希望这会有所帮助。

Content-Type: application/json

【讨论】:

  • 感谢您的回复。我试过了,但没有用。现在我已经发布了完整的代码。响应显示“正常”状态,但快速查看显示为空。请检查您是否有任何指示
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 2017-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多