如何正确提供PDF文件 [英] How to properly serve a PDF file

查看:93
本文介绍了如何正确提供PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.NET 3.5 ASP.NET。目前,我的网站以以下方式提供PDF文件:



context.Response.WriteFile(@c:\blah\blah .pdf);



这很好用。但是,我想通过 context.Response.Write(char [],int,int)方法来提供。



所以我试图通过

  byte [] byteContent = File.ReadAllBytes(ReportPath)发送文件; 
ASCIIEncoding encoding = new ASCIIEncoding();
char [] charContent = encoding.GetChars(byteContent);
context.Response.Write(charContent,0,charContent.Length);

没有工作(例如浏览器的PDF插件抱怨文件已损坏)。



所以我尝试了Unicode方法:

  byte [] byteContent = File.ReadAllBytes (ReportPath); 
UnicodeEncoding encoding = new UnicodeEncoding();
char [] charContent = encoding.GetChars(byteContent);
context.Response.Write(charContent,0,charContent.Length);

哪些也不行。



我缺少什么?

解决方案

您不应将字节转换为字符,这就是为什么它变成损坏。即使ASCII字符以字节存储,实际的ASCII字符集也限制为7位。因此,使用 ASCIIEncoding 转换字节流将会有效地从每个字节中删除第8位。



字节应写入 Response实例的OutputStream 流。



而不是加载所有字节从文件前端,可能会消耗大量内存,从流中读取文件块是一个更好的方法。以下是一个如何从一个流中读取然后写入另一个流的示例:

  void LoadStreamToStream(Stream inputStream,Stream outputStream)
{
const int bufferSize = 64 * 1024;
var buffer = new byte [bufferSize];

while(true)
{
var bytesRead = inputStream.Read(buffer,0,bufferSize);
if(bytesRead> 0)
{
outputStream.Write(buffer,0,bytesRead);
}
if((bytesRead == 0)||(bytesRead< bufferSize))
break;
}
}

然后可以使用此方法加载你的文件直接到Response.OutputStream

  LoadStreamToStream(fileStream,Response.OutputStream); 

更好的是,这里是打开文件并将其内容加载到流中的方法:

  void LoadFileToStream(string inputFile,Stream outputStream)
{
using(var streamInput = new FileStream(inputFile,FileMode。打开,FileAccess.Read))
{
LoadStreamToStream(streamInput,outputStream);
streamInput.Close();
}
}


I am using .NET 3.5 ASP.NET. Currently my web site serves a PDF file in the following manner:

context.Response.WriteFile(@"c:\blah\blah.pdf");

This works great. However, I'd like to serve it via the context.Response.Write(char [], int, int) method.

So I tried sending out the file via

byte [] byteContent = File.ReadAllBytes(ReportPath);
ASCIIEncoding encoding = new ASCIIEncoding();
char[] charContent = encoding.GetChars(byteContent);
context.Response.Write(charContent, 0, charContent.Length);

That did not work (e.g. browser's PDF plugin complains that the file is corrupted).

So I tried the Unicode approach:

byte [] byteContent = File.ReadAllBytes(ReportPath);
UnicodeEncoding encoding = new UnicodeEncoding();
char[] charContent = encoding.GetChars(byteContent);
context.Response.Write(charContent, 0, charContent.Length);

which also did not work.

What am I missing?

解决方案

You should not convert the bytes into characters, that is why it becomes "corrupted". Even though ASCII characters are stored in bytes the actual ASCII character set is limited to 7 bits. Thus, converting a byte stream with the ASCIIEncoding will effectively remove the 8th bit from each byte.

The bytes should be written to the OutputStream stream of the Response instance.

Instead of loading all bytes from the file upfront, which could possibly consume a lot of memory, reading the file in chunks from a stream is a better approach. Here's a sample of how to read from one stream and then write to another:

void LoadStreamToStream(Stream inputStream, Stream outputStream)
{
    const int bufferSize = 64 * 1024;
    var buffer = new byte[bufferSize];

    while (true)
    {
        var bytesRead = inputStream.Read(buffer, 0, bufferSize);
        if (bytesRead > 0)
        {
            outputStream.Write(buffer, 0, bytesRead);
        }
        if ((bytesRead == 0) || (bytesRead < bufferSize))
            break;
    }
}

You can then use this method to load the contents of your file directly to the Response.OutputStream

LoadStreamToStream(fileStream, Response.OutputStream);

Better still, here's a method opening a file and loading its contents to a stream:

void LoadFileToStream(string inputFile, Stream outputStream)
{
    using (var streamInput = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
    {
        LoadStreamToStream(streamInput, outputStream);
        streamInput.Close();
    }
}

这篇关于如何正确提供PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆