【问题标题】:how to load a pdf from server as an aspx page (or loading pdf files securely)?如何从服务器加载 pdf 作为 aspx 页面(或安全加载 pdf 文件)?
【发布时间】:2011-01-19 00:36:26
【问题描述】:

我有一个包含 pdf 的文件夹,但我不希望它们公开(比如只输入 www.domain.com/pdfs/doc.pdf)。

我需要他们采取某种安全措施(例如 www.domain.com/loadpdf.asmx?key=23452ADFASD12345 或使用 POST)

我该怎么做?,我知道如何创建 pdf,但不知道如何从服务器加载。

谢谢。

【问题讨论】:

  • 您不想知道文件名吗?
  • 无需让用户过多了解您存储 PDF 的方式!
  • 如果他们选择保存 pdf,显示文件名可能会很好......我没想到

标签: c# .net asp.net pdf


【解决方案1】:

将 PDF 读入字节数组并使用它。正如 awright18 所说,在处理程序(.ashx)中执行此操作。像这样的:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MapHandler : IHttpHandler, IReadOnlySessionState
{

    public void ProcessRequest(HttpContext context) {
        CreateImage(context);
    }

    private void CreateImage(HttpContext context) {

        string documentFullname = // Get full name of the PDF you want to display...

        if (File.Exists(documentFullname)) {

            byte[] buffer;

            using (FileStream fileStream = new FileStream(documentFullname, FileMode.Open, FileAccess.Read, FileShare.Read))
            using (BinaryReader reader = new BinaryReader(fileStream)) {
                buffer = reader.ReadBytes((int)reader.BaseStream.Length);
            }

            context.Response.ContentType = "application/pdf";
            context.Response.AddHeader("Content-Length", buffer.Length.ToString());
            context.Response.BinaryWrite(buffer);
            context.Response.End();

        } else {
            context.Response.Write("Unable to find the document you requested.");
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

我发现this thread 在这里非常有用,但以上内容应该对你有用。

【讨论】:

  • 很酷,谢谢,只有一个问题,如果用户选择保存文件,这会保留文件名吗?
  • 很高兴为您提供帮助。不,它不保留文件名。处理程序的类名似乎是默认文件名。我打算看看是否可以将默认文件名更改为更有意义的名称。如果您找到了这样做的方法(或者如果有人知道),请发布!
【解决方案2】:

您需要使用自定义 http 处理程序来处理这些请求。 Here 是一篇涵盖您的确切问题的文章。

【讨论】:

    猜你喜欢
    • 2019-06-28
    • 1970-01-01
    • 2021-07-05
    • 2012-11-08
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-30
    相关资源
    最近更新 更多