为什么我的 PDF 请求无法使用“请求”?
我们正在尝试创建一个 .NET aspx 页面,其中包含 PDF。通过硬编码来做到这一点很容易。
<object height="1250px" width="100%" type="application/pdf" data="our.pdf">
<param value="our.pdf" name="src" />
<param value="transparent" name="wmode" />
</object>
(不要太担心透明的事情......我们这样做是出于其他原因......但我将其包含在这里“以防万一”。)
问题是当我们想要动态生成 PDF 时。我们在前端填充文字的代码如下所示:
ltrPDF.Text = String.Format("<object height=\"1250px\" width=\"100%\" type=\"application/pdf\" data=\"ourPdfGenerator.aspx?var0={0}&var1={1}&var2={2}\">", var0, var1, var2);
ltrPDF.Text += String.Format("<param value=\"ourPdfGenerator.aspx?var0={0}&var1={1}&var2={2}\">", var0, var1, var2);
ltrPDF.Text += "<param value=\"transparent\" name=\"wmode\"/>";
ltrPDF.Text += "</object>";
有点难看,但看起来应该可以工作。但事实并非如此。
当我调试并在 PdfGenerator.aspx.cs Page_Load 方法的第一行放置断点时,我毫无困难地到达断点。然而,我们做的第一件事是尝试使用Request.QueryString:
string var0 = Request.QueryString["var0"];
它会立即抛出一个HttpException:“请求在此上下文中不可用。”我不清楚:
- 为什么不可用?
- 我能做什么呢?
编辑:(顺便说一句,我知道从 aspx 页面请求 mime 类型的 pdf 似乎有点奇怪......但是我们之前使用过 aspx 页面来生成 cs 页面......我们做了类似的事情this:
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=\"our.pdf\"");
using (MemoryStream pdfStream = new MemoryStream())
{
ourSpecialPdfGenerator.ExportToPdf(pdfStream);
Response.BinaryWrite(pdfStream.ToArray());
}
Response.End();
这在其他上下文中已经运行良好一段时间了...但始终作为其自己的页面,我们现在所做的不同之处是我们尝试嵌入它,而不是直接调用该页面,所以它是这样的。从
We're trying to create a .NET aspx page that will have a PDF within it. Doing this by hardcoding it is easy.
<object height="1250px" width="100%" type="application/pdf" data="our.pdf">
<param value="our.pdf" name="src" />
<param value="transparent" name="wmode" />
</object>
(don't worry too much about the transparent thing...we're doing that for other reasons...but I include it here "just in case".)
The problem is when we want to generate the PDF dynamically. Our code to populate the literal on the front end looks like this:
ltrPDF.Text = String.Format("<object height=\"1250px\" width=\"100%\" type=\"application/pdf\" data=\"ourPdfGenerator.aspx?var0={0}&var1={1}&var2={2}\">", var0, var1, var2);
ltrPDF.Text += String.Format("<param value=\"ourPdfGenerator.aspx?var0={0}&var1={1}&var2={2}\">", var0, var1, var2);
ltrPDF.Text += "<param value=\"transparent\" name=\"wmode\"/>";
ltrPDF.Text += "</object>";
Kind of ugly, but it seems like it should work. But it doesn't.
When I debug, and put a breakpoint on the first line of ourPdfGenerator.aspx.cs Page_Load method, I reach the breakpoint without any difficulty. However, the first thing we do is try to use Request.QueryString:
string var0 = Request.QueryString["var0"];
which immediately throws an HttpException: "Request is not available in this context." I'm not clear on:
- Why isn't it available?
- What can I do about it?
EDIT: (as an aside, I know it seems a bit weird to ask for a mime-type of pdf from a aspx page...but we've used an aspx page to generate cs pages before...we do something like this:
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=\"our.pdf\"");
using (MemoryStream pdfStream = new MemoryStream())
{
ourSpecialPdfGenerator.ExportToPdf(pdfStream);
Response.BinaryWrite(pdfStream.ToArray());
}
Response.End();
And this has been working fine in other contexts for a while...but always as its own page. What we're doing differently now is instead of having this page called directly, we're trying to embed it, so it's being called from the <object>
tag, which is apparently causing problems...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误位于
ourPdfGenerator.aspx.cs
中。您的object
标记没问题。您可以将代码发布到ourPdfGenerator.aspx.cs
吗?The error is in
ourPdfGenerator.aspx.cs
. Yourobject
tag is fine. Can you post the code toourPdfGenerator.aspx.cs
?