【问题标题】:Saving pdf stream in a file as a pdf将文件中的 pdf 流另存为 pdf
【发布时间】:2020-04-30 06:23:18
【问题描述】:

我有一个保存 pdf 流的变量,该变量的类型为 System.Threading.Tasks.Task<Stream>。我想将此 pdf 流保存在 pdf 文件中,但我不知道该怎么做。下面是我尝试处理的一段代码。关于我可以尝试将此流保存在文件中的任何想法

System.Threading.Tasks.Task<Stream> pdf = //Some logic here which gets a pdf stream

我想将变量中的pdf内容存储为pdf文件

为此我写了这个方法

public static void SaveStreamAsFile(string filePath, System.Threading.Tasks.Task<Stream> inputStream, string fileName)
{

    string path = Path.Combine(filePath, fileName);
    using (FileStream outputFileStream = new FileStream(path, FileMode.Create))
    {
       // logic
    }
}

【问题讨论】:

    标签: c# .net file pdf stream


    【解决方案1】:

    读取输入流并将其写入输出流..

    public static async Task SaveStreamAsFile(string filePath, System.Threading.Tasks.Task<Stream> inputStream, string fileName)
    {
        var stream = await inputStream;
        var path = Path.Combine(filePath, fileName);
        var bytesInStream = new byte[stream.Length];
    
        await stream.ReadAsync(bytesInStream, 0, (int) bytesInStream.Length);
    
        using (var outputFileStream = new FileStream(path, FileMode.Create))
        {
           await outputFileStream.WriteAsync(bytesInStream, 0, bytesInStream.Length);
        }
    }
    

    【讨论】:

    • 在 while 循环中,您认为 input.Read 中的输入和 outPuttFileStream.write 中的字节是什么?
    • 我已经更新了我的答案以阅读整个流并在没有夹头的情况下编写它..
    • 知道了,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    相关资源
    最近更新 更多