2017-11-11 198 views
-1

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; 
} 
+0

你能分享一下你的代碼嗎? –

+0

@Div新增評論。 – user3711357

回答

0

實測值使用文件的Base64String轉化率和溶液包括入JSON響應。

謝謝