如何将数据转换为pdf文件,然后插入zip并下载

时间:2015-08-11 07:36:20

标签: c# asp.net oracle

我从数据库二进制数据得到它实际上是PDF文件

System.Data.OracleClient.OracleDataReader reader = command.ExecuteReader();

这个读者需要阅读3次,因为我有3个pdf文件。

while (reader.Read() && !reader.IsDBNull(1))
{
   byte[] BytesData = (byte[])reader["PDF_DATA"];
}

如何将二进制数据转换为pdf,然后将所有文件插入ZIP文件,然后下载?

1 个答案:

答案 0 :(得分:1)

如果数据库中的字节是从PDF文件中获取的,那么它们的格式正确,并且不需要任何转换。

你需要一个ZIP库,比如SharpZipLib,然后你可以将每个文件作为Stream添加到它。

使用SharpZipLib,您可以修改数据库读取方法,如下所示:

        var i = 0;
        using (var zipFile = new ZipFile(""))
        {
            var factory = new ZipEntryFactory();

            while (reader.Read() && !reader.IsDBNull(1))
            {
                byte[] BytesData = (byte[])reader["PDF_DATA"];
                zipFile.Add(new DbFileSource(BytesData), "file-" + i);
                i++;
            }
        }

您还需要创建一个基本数据源类,以使上述代码有效:

    public class DbFileSource : IStaticDataSource
    {
        public DbFileSource(byte[] bytes)
        {
            _bytes = bytes;
        }

        private readonly byte[] _bytes;

        public Stream GetSource()
        {
            return new MemoryStream(_bytes);
        }
    }
警告:我还没试过,但希望这对你来说是一个很好的起点。