【发布时间】:2018-03-12 06:36:55
【问题描述】:
问题
我正在尝试发布 API 以将数据发送到 API,该 API 调用我的内部 API 服务将该数据发送到其他 API i 服务。实体包含带有文件的属性。这仅将文件发送到另一个派生,但 NameSender 属性不与文件一起发送。
实体
public class Email
{
public string NameSender{ get; set; }
public List<IFormFile> Files { get; set; }
}
API
[Consumes("multipart/form-data")]
[HttpPost]
public IActionResult SendEmail([FromForm]Entity entity)
{
try
{
string Servicesfuri = this.serviceContext.CodePackageActivationContext.ApplicationName + "/" + this.configSettings.SendNotificationServiceName;
string proxyUrl = $"http://localhost:{this.configSettings.ReverseProxyPort}/{Servicesfuri.Replace("fabric:/", "")}/api/values/Send";
//attachments
var requestContent = new MultipartFormDataContent();
foreach (var item in entity.Files)
{
StreamContent streamContent = new StreamContent(item.OpenReadStream());
var fileContent = new ByteArrayContent(streamContent.ReadAsByteArrayAsync().Result);
requestContent.Add(fileContent, item.Name, item.FileName);
}
HttpResponseMessage response = this.httpClient.PostAsync(proxyUrl, requestContent).Result;
if (response.StatusCode != System.Net.HttpStatusCode.OK)
{
return this.StatusCode((int)response.StatusCode);
}
return this.Ok(response.Content.ReadAsStringAsync().Result);
}
catch (Exception e)
{
throw e;
}
}
【问题讨论】:
标签: c# asp.net-core-2.0 asp.net-core-webapi