试图用流一个asp.net PDF文件是生产和QUOT;损坏的文件" [英] Trying to stream a PDF file with asp.net is producing a "damaged file"
问题描述
在我的asp.net web应用程序一个我需要隐藏被送达给用户一个PDF文件的位置。
于是,我写了从一个CMS系统上的位置检索其二进制内容,然后一个方法刷新字节数组Web用户。
我下载流时获得,遗憾的是,一个错误:无法打开该文件,因为它是damadged(或类似的东西,打开Adobe Reader中的文件时)
问题1:我究竟做错了什么?
问题2:我可以下载使用这种方法大文件
私人无效StreamFile(的iItem documentItem)
{
// CMS厂商特定的API
BinaryContent itemBinaryContent = documentItem.getBinaryContent();
//普通老式.NET
流FILESTREAM = itemBinaryContent.getContentStream();
变种LEN = itemBinaryContent.getContentLength();
SendStream(FILESTREAM,LEN,itemBinaryContent.getContentType());
} 私人无效SendStream(流流,诠释contentLen,字符串的contentType)
{
Response.ClearContent();
Response.ContentType = contentType中;
Response.AppendHeader(内容处置的String.Format(内联;文件名= file.pdf));
Response.AppendHeader(内容长度,contentLen.ToString());
VAR字节=新的字节[contentLen]
stream.Read(字节,0,contentLen);
stream.Close();
Response.BinaryWrite(字节);
Response.Flush();
}
下面是我使用的方法。这回传附件,因此IE产生打开/保存对话框。我也碰巧知道该文件将不大于1M,所以我敢肯定有一个更清洁的方式来做到这一点。
我和PDF文件类似的问题,我意识到,我绝对必须使用二进制流和的ReadBytes。凡是用绳子把事情搞糟了。
流流= GetStream(); //假设你有一个做到这一点的方法。
BinaryReader读卡器=新BinaryReader(流);HTT presponse响应= HttpContext.Current.Response;
response.ContentType =应用程序/ PDF
response.AddHeader(内容处置,附件;文件名= file.pdf);
response.ClearContent();
response.OutputStream.Write(reader.ReadBytes(1000000),0,1000000);//结束由页处理器prevent进一步工作的响应。
到Response.End();
In one of my asp.net web applications I need to hide the location of a pdf file being served to the users.
Thus, I am writing a method that retrieves its binary content from its location on a CMS system and then flushes a byte array to the web user.
I'm getting, unfortunately, an error when downloading the stream: "Could not open the file because it is damadged" (or something similar to that, when opening the file in adobe reader).
Question 1: what am I doing wrong? Question 2: can I download large files using this approach?
private void StreamFile(IItem documentItem)
{
//CMS vendor specific API
BinaryContent itemBinaryContent = documentItem.getBinaryContent();
//Plain old .NET
Stream fileStream = itemBinaryContent.getContentStream();
var len = itemBinaryContent.getContentLength();
SendStream(fileStream, len, itemBinaryContent.getContentType());
}
private void SendStream(Stream stream, int contentLen, string contentType)
{
Response.ClearContent();
Response.ContentType = contentType;
Response.AppendHeader("content-Disposition", string.Format("inline;filename=file.pdf"));
Response.AppendHeader("content-length", contentLen.ToString());
var bytes = new byte[contentLen];
stream.Read(bytes, 0, contentLen);
stream.Close();
Response.BinaryWrite(bytes);
Response.Flush();
}
Here is a method I use. This passes back an attachment, so IE produces an Open/Save dialog. I also happen to know that the files will not be larger than 1M, so I'm sure there's a cleaner way to do this.
I had a similar problem with PDFs, and I realized that I absolutely had to use Binary streams and ReadBytes. Anything with strings messed it up.
Stream stream = GetStream(); // Assuming you have a method that does this.
BinaryReader reader = new BinaryReader(stream);
HttpResponse response = HttpContext.Current.Response;
response.ContentType = "application/pdf";
response.AddHeader("Content-Disposition", "attachment; filename=file.pdf");
response.ClearContent();
response.OutputStream.Write(reader.ReadBytes(1000000), 0, 1000000);
// End the response to prevent further work by the page processor.
response.End();
这篇关于试图用流一个asp.net PDF文件是生产和QUOT;损坏的文件"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!