[英]Convert byte[] to file stream without writing to disk
We are getting a input from a web service as a byte[]
(which we process internally) and we need to upload to another web service which accepts only a file stream. 我们从Web服务获取一个输入作为
byte[]
(我们在内部处理),我们需要上传到另一个仅接受文件流的Web服务。
How can i to convert byte[]
to a file stream without writing to disk in C#? 如何在不使用C# 写入磁盘的情况下将
byte[]
转换为文件流?
Edit: This is not duplicate. 编辑:这不是重复的。 I am not asking how to convert
byte[]
to memory stream or file steam. 我不是在问如何将
byte[]
转换为内存流或文件流。 I am asking how to convert byte[]
to file stream without writing to disk . 我在问如何在不写入disk的情况下将
byte[]
转换为文件流。 Please note that, I need to send the data as file steam to a third party web service, which I do not have access. 请注意,我需要将数据作为文件流发送到我无权访问的第三方Web服务。 This web service accepts only as file stream.
此Web服务仅接受作为文件流。
So far I have below code: 到目前为止,我有以下代码:
string fileWritePath = "c:\\temp\\test.docx";
//here fileContent is a byte[]
File.WriteAllBytes(fileWritePath, fileContent);
FileStream fileStream = new FileStream(fileWritePath, FileMode.Open, FileAccess.Read);
I do not want to write the file to local disk and create file stream. 我不想将文件写入本地磁盘并创建文件流。
Use MemoryStream
: 使用
MemoryStream
:
using(var stream = new MemoryStream(byteArray)){
SendStreamToService(stream);
}
If you are bound to a file stream, you have to use it. 如果绑定到文件流,则必须使用它。 If you don't want to go through a physical hard drive you can install a ram disc on your system that maps parts of the memory to a virtual drive and use this drive to map your
FileStream
to. 如果您不想通过物理硬盘驱动器,则可以在系统上安装将内存的一部分映射到虚拟驱动器的ram光盘 ,然后使用该驱动器将
FileStream
映射到该磁盘。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.