Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将数据转换为pdf文件,然后插入到zip并下载_C#_Asp.net_Oracle - Fatal编程技术网

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

C# 如何将数据转换为pdf文件,然后插入到zip并下载,c#,asp.net,oracle,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格式,

我从数据库中得到的二进制数据实际上是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文件,然后下载?

如果数据库中的字节是从pdf文件中获取的,则它们的格式已经正确,不需要任何转换

您需要一个ZIP库,比如SharpZipLib,然后您可以将每个文件作为流添加到其中

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

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

警告:我还没有尝试过这个,但希望这对您来说是一个很好的起点。

当您说“下载”时,这是什么类型的应用程序?