我想從服務器下載圖像並在瀏覽器中顯示它們。但是當我在瀏覽器中輸入url(localhost:port/api/service/imageID)時,下載框會出現,要求我保存或打開圖像。但我希望圖像能夠直接顯示在瀏覽器中。 這是我的控制器「獲得」的方法:ASP .net Web Api下載和查看圖像
public HttpResponseMessage Get(int id)
{
HttpResponseMessage response;
var image = _repository.RetrieveImage(id);
if (image == null)
{
response = new HttpResponseMessage(HttpStatusCode.NotFound);
}
else
{
response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(new MemoryStream(image.ImageData));
response.Content = new ByteArrayContent(image.ImageData);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = image.OriginalFileName;
response.Content.Headers.ContentType = new MediaTypeHeaderValue(image.Mime);
response.Content.Headers.ContentLength = image.ImageData.Length;
}
return response;
非常感謝您的幫助
我不禁注意到,你指定'Content'性能的兩倍。你有沒有嘗試過使用'StreamContent'? – 2013-02-19 00:09:17
是的,我做了,但它不起作用。我會嘗試從DigitalID – aimyas 2013-02-19 00:16:42