4

サーバーから画像をダウンロードしてブラウザで表示したい。しかし、ブラウザーに URL (localhost:port/api/service/imageID) を入力すると、ダウンロード ボックスが表示され、画像を保存するか開くかを尋ねられます。しかし、ブラウザに画像をそのまま表示したいのです。これは私のコントローラーの「Get」メソッドです:

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;

助けてくれてありがとう

4

3 に答える 3

4

「添付」コンテンツ ディスポジション ヘッダーは使用しないでください。そのヘッダーを使用すると、指定されたファイルをインラインで表示する代わりにダウンロードするようブラウザに指示します。

 response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
于 2013-02-19T00:07:55.457 に答える
0

あなたのシナリオでは、StreamContentを返し、このコンテンツの適切なコンテンツタイプヘッダーを提供することができると思います。(例:image / jpeg)

于 2013-02-19T00:15:59.107 に答える