c# - ASP .net Web Api 下载和查看图片

标签 c# rest asp.net-web-api

我想从服务器下载图片并在浏览器中显示。但是当我在浏览器中输入 url (localhost:port/api/service/imageID) 时,会出现下载框询问我是保存还是打开图像。但我希望图像直接显示在浏览器中。 这是我的 Controller “获取”方法:

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;

非常感谢您的帮助

最佳答案

不要使用“附件”内容处置 header 。使用该 header 指示浏览器下载指定文件而不是内联显示它。

 response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");

关于c# - ASP .net Web Api 下载和查看图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14947574/

相关文章:

C# 加载项 Excel : ActiveSheet

rest - 使用 WADL 的原因是什么?

java - 在 JBoss 7.5.0 上使用 REST 服务时,XML 文件处理不正确

c# - C# Web Api 中的动态注册 Controller

c# - 转换 Web API 以使用自托管

c# - PageRequestManagerServerErrorException 对象引用未设置... ASP.net AJAX

c# - 使用 Visual Studio 2010 时 32 位和 64 位操作系统之间的性能差异?

c# - 自动向 Rebus 消息添加 header

c# - 适合初学者的简单 c# 练习。我卡住了

c# - 使用泛型接口(interface)的 WCF RESTful 服务