使用 c# 将 FileStream 编码为 base64 [英] Encode a FileStream to base64 with c#

查看:126
本文介绍了使用 c# 将 FileStream 编码为 base64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何将一个简单的字符串编码/解码为 base64.

I know how to encode / decode a simple string to / from base64.

但是如果数据已经写入FileStream 对象,我该怎么做.假设我只能访问 FileStream 对象,而不能访问以前存储在其中的原始数据.在将 FileStream 刷新到文件之前,我将如何将 FileStream 编码为 base64.

But how would I do that if the data is already been written to a FileStream object. Let's say I have only access to the FileStream object not to the previously stored original data in it. How would I encode a FileStream to base64 before I flush the FileStream to a file.

Ofc 在我将 FileStream 写入文件后,我可以打开我的文件并对其进行编码/解码,但我想一步完成这一切,而不是一个接一个地执行两个文件操作.文件可能更大,而且在之前刚刚保存了一小段时间后,加载、编码和再次保存它也需要双倍的时间.

Ofc I could just open my file and encode / decode it after I have written the FileStream to the file, but I would like to do this all in one single step without doing two file operations one after another. The file could be larger and it would also take double time to load, encode and save it again after it was just saved a short time before.

也许你们中有人知道更好的解决方案?我可以将 FileStream 转换为字符串,对字符串进行编码,然后将字符串转换回 FileStream 例如,或者我会做什么以及这样的代码会是什么样子?

Maybe someone of you knows a better solution? Can I convert the FileStream to a string, encode the string and then convert the string back to a FileStream for example or what would I do and how would such a code look like?

推荐答案

您还可以将字节编码为 Base64.如何从流中获取此信息,请参见此处:如何在 C# 中将 Stream 转换为 byte[]?

You can also encode bytes to Base64. How to get this from a stream see here: How to convert an Stream into a byte[] in C#?

或者我认为也应该可以使用 .ToString() 方法并对其进行编码.

Or I think it should be also possible to use the .ToString() method and encode this.

这篇关于使用 c# 将 FileStream 编码为 base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆