如何正确地服务​​于PDF文件 [英] How to properly serve a PDF file

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

问题描述

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

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.pdf);

这个伟大的工程。不过,我想通过 context.Response.Write(的char [],INT,INT)的方法来处理它。

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

所以,我想通过

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

这不工作(如浏览器的插件PDF抱怨说,该文件已损坏)。

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

所以,我想在UNI code方法:

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);

这也没有工作。

我是什么失踪?

推荐答案

您不应该将字节转换成字符,这就是为什么它成为损坏。尽管ASCII字符被存储在字节实际ASCII字符集被限制为7位。因此,转换与的ASCIIEncoding 将有效从每一个字节删除了第8位。

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.

中的字节应写入 OutputStream的流响应实例。

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;
    }
}

您可以使用这个方法到您的文件中的内容直接加载到Response.OutputStream

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天全站免登陆