2017-08-07 49 views
0

我有一个函数下面,用于返回图像和身份证。在测试时,我看不到我的ID被返回,而是我只能看到在浏览器中返回的图像(可能由于它完全是字节数据)。在铬HttpGet返回图像和身份证

  int id = 0; 
      using (Bitmap bitmap = Image.Generate(context, out id)) 
      { 
       HttpResponse response = context.Response; 
       response.CacheControl = "no-cache"; 
       response.ContentType = "image/JPG"; 
       bitmap.Save(response.OutputStream, ImageFormat.Jpeg); 
       response.End(); 

       var responseModel = new ResponseModel 
       { 
        Id = id, 
        Image = bitmap 
       }; 

       return Ok(responseModel); 
      }; 

响应预览: enter image description here

这是返回响应的适当的方式,如果有包括图像?如果是这样,我如何从ajax调用获取响应数据,包括图像和ID(可以使用Json.parse完成吗?但这是bytedata)?

回答

1

有一两件事你可以做的是将ID添加在HttpResponseMessage的标题很喜欢这个答案

Asp.net Web API return File with metadata about the file

我移动,所以我会在以后更新我的答案,但本质上,你只会有图像是消息的正文和标题中的任何元数据。然后在客户端解析出响应头以获取所需内容。我过去曾用过这个。如许

样品

public HttpResponseMessage Get() 
{ 
    XDocument xDoc = GetXMLDocument(); 
    int id = CallToGetMyId(); 

    var response = this.Request.CreateResponse(
     HttpStatusCode.OK, 
     xDoc.ToString(), 
     this.Configuration.Formatters.XmlFormatter 
    ); 
    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") 
    { 
     FileName = "image.png" 
    }; 
    response.Headers.Add("Id", id); 
    return response; 
}