我尝试将文件上传请求从一个Web API服务重新发布到另一个后端API服务。
代码类似于:
// exposed public API:
[HttpPost]
public async Task<HttpResponseMessage> UploadImage()
{
var client = new HttpClient();
HttpResponseMessage response = await client.PostAsync(backendAddress + "api/file/upload",
Request.Content // this is the incoming HttpRequestMessage.Content which appears intact with the file
);
// throws a 500 error before getting to the backend service
...
// private backend API
[HttpPost]
public async Task<HttpResponseMessage> UploadImage()
{
try
{
var provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
...
我这样做的原因是前端框与客户端通信,后端框写入数据库。