【发布时间】: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