在浏览器中只显示PDF一次,然后将其从C#服务器中删除
本文关键字:服务器 删除 然后 浏览器 显示 PDF 一次 | 更新日期: 2023-09-27 18:21:04
目前我们有一个生成PDF的应用程序,它直接加载到响应对象的页面中。将原始文件从临时位置删除,以避免再次访问PDF。
MyFileStream = new FileStream(fileName, FileMode.Open);
FileSize = MyFileStream.Length;
if (FileSize > 0)
{
byte[] Buffer = new byte[(int)FileSize];
MyFileStream.Read(Buffer, 0, (int)FileSize);
MyFileStream.Close();
// Delete the pdf file
File.Delete(fileName);
if (Buffer != null)
{
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Type", "application/pdf");
Response.AddHeader("Content-Disposition", "inline;filename=" + fileName);
Response.BinaryWrite(Buffer);
Response.Flush();
}
}
else
{
Response.Write("File not found, or file is empty.");
Response.Flush();
}
我想修改页面,将PDF加载到页面内的DIV中,这样我就可以在同一页面上添加另一个交互式元素,比如触发其他内容的按钮。
我看到嵌入PDF的方法是使用类似的东西
<div class="pdf">
<object data="myfile.pdf" type="application/pdf" width="100%" height="100%">
<p>It appears you don't have a PDF plugin for this browser.
No biggie... you can <a href="myfile.pdf">click here to
download the PDF file.</a></p>
</object>
</div>
<div class="buttons">
This is where I will have other links or buttons to trigger some functions.
</div>
然而,这似乎指向来自服务器的静态PDF资源myfile.PDF。
我的问题:如何确保打开此页面后,静态PDF不可用于直接链接
(类似于我的原始代码,一旦页面显示并流式传输到响应页面,静态文件就会被删除)
您可以编写一个aspx页面,将PDF内容作为字节流写入请求者。它需要指定输出类型为application/pdf。例如,如果您有一个名为getpdf.aspx
的页面,它可以接受参数pdf_id
。然后,您可以使用参数值引用该页面,它将动态加载pdf,而不会写入服务器文件系统。
然后,在您的应用程序页面中,您可以有一个div或iframe,它引用具有请求的pdf_id
:的getpdf.aspx
页面
<iframe src="getpdf.aspx?pdf_id=1234" height="100%" width="100%"></iframe>
感谢Gmiley和Eric。。。
根据你的建议,我做了更多的研究,找到了我想要的。。。使用嵌入或免费库来做我需要的事情。
<html>
<head id="Head1" runat="server">
<title>PDF Documents</title>
<script type="text/javascript" src="/Scripts/pdfobject.js"></script>
<script type="text/javascript">
window.onload = function () {
console.log("Loading ObjectPDF");
var pdf = new PDFObject({
url: "<%= this.ResolveUrl("~/Pages/GetPDF.aspx?PdfFileName=" + pdfFile)%>",
id: "pdfRendered",
pdfOpenParams: {
view: "FitH"
}
}).embed("pdfRendered");
console.log("finished Embed");
console.dir(pdf);
};
</script>
</head>
<body>
<div id="pdfRendered" style="height: 90%">
<p>
It appears you don't have a PDF plugin for this browser. <a href="<%= this.ResolveUrl("~/Pages/GetPDF.aspx?PdfFileName=" + pdfFile)%>">Click here to download the PDF file.</a>
</p>
</div>
<div id="eSign" style="background: #ccc;">SIgnature Trigger here</div>
</body>
</html>