PDF ファイルをダウンロードするのではなく、新しいタブまたはウィンドウで開く方法 (asp.net を使用) 質問する

PDF ファイルをダウンロードするのではなく、新しいタブまたはウィンドウで開く方法 (asp.net を使用) 質問する

これはファイルをダウンロードするためのコードです。

System.IO.FileStream fs = new System.IO.FileStream(Path+"\\"+fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] ar = new byte[(int)fs.Length];
fs.Read(ar, 0, (int)fs.Length);
fs.Close();

Response.AddHeader("content-disposition", "attachment;filename=" + AccNo+".pdf");
Response.ContentType = "application/octectstream";
Response.BinaryWrite(ar);
Response.End();

このコードが実行されると、ユーザーにファイルを開くか保存するかを尋ねます。代わりに、新しいタブまたはウィンドウを開くファイルを表示します。どうすればこれを実現できますか?

注記:

ファイルは必ずしも Web サイトのフォルダにあるとは限りません。別のフォルダにある場合もあります。

ベストアンサー1

Response.ContentType = contentType;
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=" + fileName);
Response.BinaryWrite(fileContent);

そして

<asp:LinkButton OnClientClick="openInNewTab();" OnClick="CodeBehindMethod".../>

JavaScript の場合:

<script type="text/javascript">
    function openInNewTab() {
        window.document.forms[0].target = '_blank'; 
        setTimeout(function () { window.document.forms[0].target = ''; }, 0);
    }
</script>

気をつけてくださいターゲットをリセットそうしないと、 などの他のすべての呼び出しがResponse.Redirect新しいタブで開き、望ましくない結果になる可能性があります。

おすすめ記事