我有一个使用文件流返回 msi 包的方法。
public FileStream DownloadMsiFileStream()
{
FileStream fs = new FileStream(@"C:\temp\test.msi", FileMode.Create, System.IO.FileAccess.ReadWrite);
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("deviceupdate");
// Retrieve reference to a blob named "KC.AttendanceManager.PrintServiceInstaller.msi".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("test.msi");
//Retrive the memorystream
blockBlob.DownloadToStream(fs);
return fs;
}
这非常完美,我可以使用 web api 方法下载文件流,将流写入文件并最终得到一个工作的 msi 包。
但是现在我想避免将文件写入服务器端的磁盘,因为它会导致并发问题。相反,我尝试将文件流更改为 Memorystream,如下所示:
public MemoryStream DownloadMsi()
{
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("deviceupdate");
// Retrieve reference to a blob named "KC.AttendanceManager.PrintServiceInstaller.msi".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("test.msi");
MemoryStream ms = new MemoryStream();
//Retrive the memorystream
blockBlob.DownloadToStream(ms);
return ms;
}
但是当我稍后尝试将流写入文件时(只是服务器端让它工作),如下所示:
MemoryStream ms = DeviceUpdateManager.GetClientUpdateMsi();
FileStream file = new FileStream(@"C:\temp\test2.msi", FileMode.OpenOrCreate);
byte[] bytes = new byte[file.Length];
file.Read(bytes, 0, (int)file.Length);
ms.Write(bytes, 0, (int)file.Length);
file.Close();
ms.Close();
结果是无效(空)msi 文件。内存流不为空,因为System.Text.Encoding.UTF8.GetString(ms.ToArray())
返回一堆。我如何最终得到一个工作的 msi?任何帮助appriciated。