如何响应文件流和json对象作为Web API的单一响应
问题描述:
Web API, 我当前的Web API正在响应具有2个属性的JSON对象,如下所示。 我想:1,字段名称:somename如何响应文件流和json对象作为Web API的单一响应
现在,需要在响应中包含一个FILE(.CERT)以及现有的JSON属性。
如何做到这一点?
[授权]
[Route("getfileAndProperties")]
public HttpResponseMessage GetTestFile()
{
HttpResponseMessage result = null;
var localFilePath = HttpContext.Current.Server.MapPath("~/timetable.cer");
var p = new Person() {Id=1,userField="name"};
//need to include this Person object into response along with file.
if (!File.Exists(localFilePath))
{
result = Request.CreateResponse(HttpStatusCode.Gone);
}
else
{
// Serve the file to the client
result = Request.CreateResponse(HttpStatusCode.OK);
result.Content = new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read));
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = "SampleImg";
}
return result;
}
答
实测值使用文件的Base64String转化率和溶液包括入JSON响应。
谢谢
你能分享一下你的代码吗? –
@Div新增评论。 – user3711357