teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

追記追加

2017/06/29 07:34

投稿

退会済みユーザー
answer CHANGED
@@ -12,4 +12,137 @@
12
12
 
13
13
  ということは、少なくとも今のやり方ではできてないということになるはずなのですが、そこを確認してください。
14
14
 
15
- (根本的にやり方を変えないとダメということになりそうな気がします)
15
+ (根本的にやり方を変えないとダメということになりそうな気がします)
16
+
17
+ ----- 2017/6/29 16:34 追記 -----
18
+
19
+ 下の 2017/06/29 15:48 の私のコメントで「自分の環境と検証に使ったコードその他の情報を回答欄に書いておきます」と書きましたが、それを以下に書いておきます。
20
+
21
+ **環境:**
22
+ Visual Studio Commnuty 2015 Update 3
23
+ ASP.NET Web Forms(Web サイトプロジェクトとして作成、IIS のサイトに設定)
24
+ .NET Framework 4.6.1
25
+ Windows 10 Pro 64-bit, 10.0.14393 ビルド 14393
26
+ IIS 10.0, CLR v4.0, 64-bit, 統合パイプラインモード
27
+ IE11 ver. 11.1358.14393.0 更新 ver. 11.0.43 (KB4021558)
28
+
29
+ **ソースコード**
30
+
31
+ **base.aspx **に該当するもの。コードビハインド形式にせず一体にしています。
32
+
33
+ ```
34
+ <%@ Page Language="C#" %>
35
+
36
+ <!DOCTYPE html>
37
+
38
+ <script runat="server">
39
+
40
+ </script>
41
+
42
+ <html xmlns="http://www.w3.org/1999/xhtml">
43
+ <head runat="server">
44
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
45
+ <title></title>
46
+ <script src="/Scripts/jquery-1.10.2.js"></script>
47
+ <script type="text/javascript">
48
+ //<![CDATA[
49
+ $(function () {
50
+ $('#btn_ok').click(function () {
51
+ window.open('0008-out.aspx', '_blank');
52
+ });
53
+
54
+ $('#btn_dame').click(function () {
55
+ window.open('', 'report');
56
+
57
+ var form = $('<form/>', {
58
+ id: 'rpt_p',
59
+ action: '0008-out.aspx',
60
+ target: 'report',
61
+ method: 'post'
62
+ });
63
+
64
+ $('body').append(form);
65
+
66
+ $('#rpt_p').submit();
67
+ $('#rpt_p').remove();
68
+ });
69
+ });
70
+ //]]>
71
+ </script>
72
+ </head>
73
+ <body>
74
+ <form id="form1" runat="server">
75
+ <div>
76
+ <h1>window.open で開いたウィンドウに pdf をダウンロード</h1>
77
+ <input type="button" id="btn_ok" value="GET" />
78
+ <input type="button" id="btn_dame" value="POST" />
79
+ </div>
80
+ </form>
81
+ </body>
82
+ </html>
83
+ ```
84
+
85
+ **out.aspx.vb** に該当するもの。言語は C# です。(ページの名前は 0008-out.aspx としています)
86
+
87
+ ```
88
+ using System;
89
+ using System.Collections.Generic;
90
+ using System.Linq;
91
+ using System.Web;
92
+ using System.Web.UI;
93
+ using System.Web.UI.WebControls;
94
+
95
+ public partial class _0008_out : System.Web.UI.Page
96
+ {
97
+ protected void Page_Load(object sender, EventArgs e)
98
+ {
99
+ Response.Cache.SetCacheability(HttpCacheability.NoCache);
100
+ Response.Cache.SetNoStore();
101
+
102
+ Response.ClearHeaders();
103
+ Response.ClearContent();
104
+ Response.ContentType = "Application/pdf";
105
+ Response.AddHeader("content-disposition", "inline; filename=sample.pdf");
106
+ string path = Server.MapPath("~/app_data/sample.pdf");
107
+ Response.TransmitFile(path);
108
+ Response.End();
109
+ }
110
+ }
111
+ ```
112
+
113
+ 上記 base.aspx の[POST]ボタンをクリックしたとき、**Fiddler で見た要求と応答**
114
+
115
+ **要求** (コンテンツは設定してないので空です)
116
+ ```
117
+ POST http://websiteproject.com/0008-out.aspx HTTP/1.1
118
+ Accept: text/html, application/xhtml+xml, image/jxr, */*
119
+ Referer: http://websiteproject.com/0008-AutoPostByScript.aspx
120
+ Accept-Language: ja
121
+ User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
122
+ Content-Type: application/x-www-form-urlencoded
123
+ Accept-Encoding: gzip, deflate
124
+ Content-Length: 0
125
+ Host: websiteproject.com
126
+ Connection: Keep-Alive
127
+ Pragma: no-cache
128
+ ```
129
+
130
+ **応答**
131
+ ```
132
+ HTTP/1.1 200 OK
133
+ Cache-Control: private
134
+ Content-Type: Application/pdf
135
+ Server: Microsoft-IIS/10.0
136
+ content-disposition: inline; filename=sample.pdf
137
+ X-AspNet-Version: 4.0.30319
138
+ X-Powered-By: ASP.NET
139
+ Date: Thu, 29 Jun 2017 07:12:49 GMT
140
+ Content-Length: 73552
141
+
142
+ %PDF-1.4
143
+ %äüöß
144
+ 2 0 obj
145
+ <</Length 3 0 R/Filter/FlateDecode>>
146
+ stream
147
+ ・・・以下省略・・・
148
+ ```