代码之家  ›  专栏  ›  技术社区  ›  sergiogx

如何以ASPX页面的形式从服务器加载PDF(或安全地加载PDF文件)?

  •  2
  • sergiogx  · 技术社区  · 16 年前

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

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

    我该怎么做?,我了解了如何创建PDF,但不了解如何从服务器加载PDF。

    谢谢。

    2 回复  |  直到 16 年前
        1
  •  2
  •   Community Mohan Dere    9 年前

    将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
  •  1
  •   awright18    16 年前

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