简体   繁体   English

如何将PDF写入asp.net页面?

[英]how can I do write a PDF to a asp.net page?

I'm trying to get a PDF page that is within the context of my website to display when a user designates. 我正在尝试获取网站上下文中的PDF页面,以在用户指定时显示。 I have the following code: 我有以下代码:

    if (context.User.Identity.IsAuthenticated)
    {
        string SampleURL = context.Request.CurrentExecutionFilePath; //CurrentExecutionFilePath;

        context.Response.Buffer = true;
        context.Response.Clear();
        using (FileStream fs = new FileStream(SampleURL,FileMode.Open)) //System.IO.File.OpenRead(path))
        {
            int length = (int)fs.Length;
            byte[] buffer;

            using (BinaryReader br = new BinaryReader(fs))
            {
                buffer = br.ReadBytes(length);
            }

            context.Response.Clear();
            context.Response.Buffer = true;
            context.Response.ContentType = "application/pdf";
            context.Response.BinaryWrite(buffer);
            context.Response.End();
        }
    }
    else
    {
        context.Response.Redirect(
           "~/Error/invalid_access.aspx");
    }

The only problem is I cannot get the PATH to work correctly. 唯一的问题是我无法使PATH正常工作。 If I was to call the PDF directly thru the URL it would be http://www.abc.com/reports/sample.pdf but I cannot get myself back to that location. 如果要直接通过URL调用PDF,它将为http://www.abc.com/reports/sample.pdf,但我无法回到该位置。 I implemented an HTTPHandler to prevent someone from going to the URL but now I need to stream the file back to a browser and write it. 我实现了一个HTTPHandler来防止某人访问URL,但是现在我需要将该文件流回浏览器并编写。

thoughts, comments, suggestions? 有想法,意见或建议吗?

edit: Its the PATH that I cannot get the relative url to point to the proper location. 编辑:这是我无法获得相对URL指向正确位置的PATH。 context.Request.CurrentExecutionFilePath gives me "/www.abc.com/sample_reports/sample.pdf" but I just can't seem to twist that around to be able to open/read it context.Request.CurrentExecutionFilePath给了我“ /www.abc.com/sample_reports/sample.pdf”,但我似乎无法将其打开或读取

What do you mean by path? 路径是什么意思? Name of the file? 文件名? You can do that with Content-Disposition header. 您可以使用Content-Disposition标头执行此操作。

Response.Addheader "Content-Disposition", "attachment;Filename=WhateverName.pdf"

You have to open it from the local server. 您必须从本地服务器打开它。 If it is located on your server you can do 如果它位于您的服务器上,则可以执行

FileStream fs = new FileStream(Server.MapPath("/example.pdf"),FileMode.Open)

If you want to load it from an URL you have to download the PDF first. 如果要从URL加载,则必须先下载PDF。

   WebClient client = new WebClient ();

    // Add a user agent header in case the 
    // requested URI contains a query.

    client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

    Stream data = client.OpenRead (args[0]);
    StreamReader reader = new StreamReader (data);
    string s = reader.ReadToEnd ();` 

I would change a few things 我会改变几件事

First I might consider switching the file reading to something easier if it's a file on disk 首先,如果磁盘上有文件,我可能会考虑将文件读取切换为更简单的内容

byte[] buffer = File.ReadAllBytes( path );

now for the context 现在就上下文

context.Response.Clear();
context.Response.ClearHeaders();
context.Response.Buffer = true;
context.Response.ContentType = "application/pdf";
context.Response.AddHeader( "content-disposition","attachment; filename=file.pdf" );
context.Response.AddHeader("Content-Length", buffer .Length.ToString() );
context.Response.BinaryWrite(buffer);
context.Response.Close();
context.Response.End();
context.Response.Flush();

Is the above code overkill? 上面的代码过度杀伤力吗? Possibly but I've found with all the different browsers running around overkill is worth it. 可能,但我发现围绕过大杀伤力运行的所有不同浏览器都是值得的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM