c# webapi 返回指定ContentType
string path = System.Web.Hosting.HostingEnvironment.MapPath(@"~/");
System.IO.FileStream fs = new System.IO.FileStream(path + "47.png", System.IO.FileMode.Open, System.IO.FileAccess.Read);
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
HttpContent content = new StreamContent(fs);
content.Headers.Add("Content-Type", "text / plain");
content.Headers.Add("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("name.png"));
result.Content = content;
return result;
[HttpGet]
public HttpResponseMessage GetID()
{
string path = System.Web.Hosting.HostingEnvironment.MapPath(@"~/");
System.IO.FileStream fs = new System.IO.FileStream(path + "47.png", System.IO.FileMode.Open, System.IO.FileAccess.Read);
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StreamContent(fs),
};
result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("text/plain");
result.Content.Headers.ContentDisposition =new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = "newWoman.png"
};
return result;
}