将pdf流另存为pdf

问题描述 投票:0回答:1

我有一个保存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个回答
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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.