gpt4 book ai didi

c# - 如何从 HTTP 流中组装 pdf?

转载 作者:可可西里 更新时间:2023-11-01 16:56:27 24 4
gpt4 key购买 nike

我正在使用第 3 方 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;
}
}
}
}
}

最佳答案

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

最好是将写入操作包含在 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);
}
}

关于c# - 如何从 HTTP 流中组装 pdf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18042568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com