我有一个保存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
}
}
读取输入流并将其写入输出流。
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);
}
}