0

ユーザーがファイルをハードドライブにダウンロードしたり、ブラウザーで表示したりできるMVC3アプリがあります。ファイルは、インターネットにアクセスできないファイルシャーに保存されます。したがって、ファイルのバイト配列はメモリに保存され、次のようにブラウザに送信されます。

            /// <summary>
    /// overrides actionresult method so document can be viewed in browser window
    /// </summary>
    /// <param name="context"></param>
    public override void ExecuteResult(ControllerContext context)
    {
        // get current context and set values
        var response = context.HttpContext.Response;
        response.Clear();
        response.BufferOutput = false;   // to prevent buffering 
        response.Cache.SetCacheability(HttpCacheability.NoCache);
        response.ContentType = ContentType;
        response.AddHeader("Content-Length", Length.ToString());

        var ext = Path.GetExtension(FileName);

        if (!Show)
        response.AddHeader("content-disposition", "attachment; filename=\"" + FileName+ "\"");

        response.ContentEncoding = System.Text.Encoding.UTF8;

        var stream = new MemoryStream(ContentBytes);
        stream.WriteTo(response.OutputStream);
        stream.Dispose();

    }

なんらかの理由でPDFを表示できません。ダウンロードはできますが、表示できません。ここにサンプルプロジェクトを添付していますshowinbrowser.zip。そのVS2010sp1。誰かがメモリに保存されている新しいブラウザウィンドウ/タブにPDFファイルを表示する方法を教えてもらえますか?ありがとう

4

2 に答える 2

2

これを処理する方法を使用してみませController.Fileんか?

public ActionResult _ShowDocument(string id)
{
  byte[] fileData=GetByteArrayFilefromId(id);
  return File(fileData,"application/pdf","somefile.pdf");
}
于 2012-08-17T18:35:45.653 に答える
1

こんにちはコンテンツタイプをpdfに設定します

//Set the appropriate ContentType.
     Response.ContentType = "Application/pdf";
于 2012-08-17T18:33:02.997 に答える