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

时间:2018-08-21 15:44:48

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

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

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

这是我的代码部分:

string filepath =“ C:/Users/O42895/Desktop/DownloadAttachmen20189t.jpg”;               字符串文件名=“ 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()。Result;                   Console.WriteLine(resBody);

我收到响应,但无法打印或阅读。请让我知道是否有人知道

1 个答案:

答案 0 :(得分:0)

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

希望这会有所帮助。

Content-Type: application/json