【问题标题】:Embedding PDF in HTML when the file is outside the application folder当文件位于应用程序文件夹之外时,在 HTML 中嵌入 PDF
【发布时间】:2012-12-08 21:40:20
【问题描述】:

我将 PDF 文件保存在 Web 应用程序外部的服务器文件夹中(出于安全原因),我需要使用 google 文档预览文件。

<iframe src="http://docs.google.com/gview?url=????&embedded=true" 
        style="width:718px; height:700px;" frameborder="0"></iframe>

直到现在,我通过以文件路径为参数的表单中的重定向使它们可供下载:

string path = Request.QueryString["src"];
int index = Request.QueryString["src"].ToString().LastIndexOf("\\");
string filename = Request.QueryString["src"].ToString().Substring(index + 1);
FileStream fileStream = new FileStream(path, FileMode.Open);
int fileSize = (int)fileStream.Length;
byte[] Buffer = new byte[fileSize];
fileStream.Read(Buffer, 0, fileSize);
fileStream.Close();
Response.ContentType = ContType(path);
Response.AddHeader("content-disposition", "attachment;filename=\"" + filename);
Response.BinaryWrite(Buffer);
Response.End();

我尝试添加 url file.aspx?src=C:\... 作为 google doc iframe 的 url 的参数,比如

http://docs.google.com/gview?url=file.aspx?src=C:\...

但它不起作用。

还有其他方法可以将 URL 分配给流吗?

【问题讨论】:

  • 如果 Google 文档是“客户端”(您不再强制客户端下载文件),则不确定您(仍然)需要额外的标头 content-disposition。嗯……
  • 问题是我不能写一个带有参数的url作为docs.google.com的参数。这就像嵌套的查询字符串...

标签: c# asp.net pdf stream google-docs


【解决方案1】:

你可以试试这样的:

<iframe src="http://docs.google.com/gview?url='file.aspx?src='C:\storage\test.pdf''&embedded=true" 
        style="width:718px; height:700px;" frameborder="0"></iframe>

根据需要将 ' 与 " 交替使用。

作为旁注,我建议您不要将完整的文件路径从服务器发送到客户端的浏览器。这听起来像是将 50 英寸电视、蓝光播放器、电脑和其他贵重物品放在您家中的位置草稿,供任何潜在的小偷考虑。

作为替代方案,您可以使用带有 ID 的数据库或 XML 文件来识别每个文件,并从那里读取文件路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    • 2018-08-28
    相关资源
    最近更新 更多