我一直在使用Azure Blob存储服务来保存/恢复要在Azure网页中托管的网页上下文中的文件.
在学习过程中,我提出了两个解决方案; 第一个基本上使用DownloadToStream哪个相同,但有一个FileStream.在这种情况下,我必须在将文件返回给用户之前将其写入服务器.
public static Stream GetFileContent(string fileName, HttpContextBase context)
{
CloudBlobContainer container = GetBlobContainer();
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
Stream fileStream = new FileStream(
context.Server.MapPath("~/App_Data/files/" + fileName), FileMode.Create);
blockBlob.DownloadToStream(fileStream);
fileStream.Close();
return File.OpenRead(context.Server.MapPath("~/App_Data/files/" + fileName));
}
public ActionResult Download(string fileName)
{
byte[] fileContent = MyFileContext.GetFileContent(fileName);
return File(fileContent, "application/zip", fileName);
}
Run Code Online (Sandbox Code Playgroud)
另一方面,我使用DownloadToByteArray函数将Blob的内容写入用Blob文件的大小初始化的字节数组中.
public static byte[] GetFileContent(string fileName)
{
CloudBlobContainer container = GetBlobContainer();
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
blockBlob.FetchAttributes();
long fileByteLength = blockBlob.Properties.Length;
byte[] fileContent = new …Run Code Online (Sandbox Code Playgroud)