如何从HTTP流中组装pdf

本文关键字:pdf HTTP | 更新日期: 2023-09-27 18:26:13

我使用的是第三方html到pdf的转换(DocRaptor)。你把你的HTML张贴到他们的网站上,他们会回复PDF。他们给你的启动代码运行良好,但它会把文件放在你的硬盘上。我修改了他们的代码,使其通过浏览器并作为文件下载。所以我100%相信我从HTTP响应中得到的数据是好数据。我似乎无法将它组装回一个可用的文件。

我有理由相信问题在于我如何处理responseStream数据。一旦我进入Try/Catch,一切似乎都错了。我对c#和web编程很陌生,所以我非常感谢so用户的指导。谢谢这是我的密码。

            string postData = String.Format(PostFormat,
            (string.IsNullOrEmpty(DocumentContent) ? "document_url" : "document_content"),
            HttpUtility.UrlEncode(string.IsNullOrEmpty(DocumentContent) ? DocumentURL : DocumentContent),
            HttpUtility.UrlEncode(Name),
            HttpUtility.UrlEncode(type),
            HttpUtility.UrlEncode(Test.ToString().ToLower()),
            HttpUtility.UrlEncode(Strict),
            HttpUtility.UrlEncode(PrinceOptions));
        var byteArray = Encoding.UTF8.GetBytes(postData);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(DocRaptorUrl);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = byteArray.Length;
        using (var dataStream = request.GetRequestStream()) { dataStream.Write(byteArray, 0, byteArray.Length); }
        System.IO.Stream stream = null;
        try 
        {
            using (HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse())
            {
                using (System.IO.Stream responseStream = httpResponse.GetResponseStream())
                {
                    var filepath = @"C:'Users'David'Downloads'UberwriterUSRReport.pdf";
                    HttpContext.Current.Response.ContentType = "application/pdf";
                    // let the browser know how to open the PDF document, attachment or inline, and the file name
                    HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=UberwriterUSRReport.pdf"));
                    stream = new System.IO.FileStream(filepath, System.IO.FileMode.Create);
                    CopyStream(responseStream, stream);
                    long bytestToRead = stream.Length;
                    while (bytestToRead > 0)
                    {
                        if (HttpContext.Current.Response.IsClientConnected)
                        {
                            byte[] buffer = new Byte[10000];  
                            int length = stream.Read(buffer, 0, 10000);
                            HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);
                            HttpContext.Current.Response.Flush();
                            bytestToRead = bytestToRead - length;
                        }
                        else
                        {
                            bytestToRead = -1;
                        }
                    }
                }
            }
        }

如何从HTTP流中组装pdf

您是否打算在将文件发送到浏览器之前将其保存到硬盘?因为这就是你现在(错误地)所做的。

最好是将写操作包含在using语句中,因为我看不到你在任何地方关闭流:

stream = new System.IO.FileStream(filepath, System.IO.FileMode.Create);

保存到文件:

CopyStream(responseStream, stream);

接下来,您将尝试读取输出流(您刚刚用它保存了文件),并将其写入Response.outputstream。您已经准备好了复制流实现,为什么要在这里手动执行?:

HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);

所以,我想说它应该是这样的:

using (HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse())
{
    using (System.IO.Stream responseStream = httpResponse.GetResponseStream())
    {
        var filepath = @"C:'Users'David'Downloads'UberwriterUSRReport.pdf";
        HttpContext.Current.Response.ContentType = "application/pdf";
        // let the browser know how to open the PDF document, attachment or inline, and the file name
        HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=UberwriterUSRReport.pdf"));
        using (var stream = new System.IO.FileStream(filepath, System.IO.FileMode.Create)) {
          CopyStream(responseStream, stream);
        }
        using (var readstream = new System.IO.FileStream(filepath, System.IO.FileMode.Read)) {
            CopyStream(readstream, HttpContext.Current.Response.OutputStream);
        }
    }
}

或者,如果你根本不想把文件保存在服务器上:

using (HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse())
{
    using (System.IO.Stream responseStream = httpResponse.GetResponseStream())
    {
        // let the browser know how to open the PDF document, attachment or inline, and the file name
        HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=UberwriterUSRReport.pdf"));
      CopyStream(responseStream, HttpContext.Current.Response.OutputStream);
    }
}

非常感谢Stephen让我走上了正确的道路。我进一步完善了执行。我有更多的代码需要。我只想让用户点击一个按钮,将HTML发布到DocRaptor.com网站,让他们用生成的PDF进行响应,然后该文件在浏览器中显示为下载文件。以下是在Azure上测试的最终实现的代码。

            try 
        {
            using (HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse())
            {
                using (System.IO.Stream responseStream = httpResponse.GetResponseStream())
                {
                    //var filepath = @"C:'Users'David'Downloads'UberwriterUSRReport.pdf";
                    HttpContext.Current.Response.Clear();
                    HttpContext.Current.Response.ContentType = "application/pdf";
                    HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("atachment; filename=UberwriterUSRReport.pdf"));
                    HttpContext.Current.Response.BufferOutput = true;
                    CopyStream(responseStream, HttpContext.Current.Response.OutputStream);
                }
            }
        }