Asp.net mvc 4 Windows Azure blob压缩并发送到客户端(ASP.NET MVC)
我有一些文件存储在WindowsAzure blob容器中(比如file1.txt和file2.txt)。 在ASP.NETMVC4应用程序(托管在azurewebsites上)中,我需要创建一个“下载为zip”链接。当用户单击它时,他会得到一个包含两个txt文件的zip文件。 我很难编写完成此任务的控制器方法:-(有人能提供简短的示例吗?Asp.net mvc 4 Windows Azure blob压缩并发送到客户端(ASP.NET MVC),asp.net-mvc-4,azure,zip,azure-storage-blobs,Asp.net Mvc 4,Azure,Zip,Azure Storage Blobs,我有一些文件存储在WindowsAzure blob容器中(比如file1.txt和file2.txt)。 在ASP.NETMVC4应用程序(托管在azurewebsites上)中,我需要创建一个“下载为zip”链接。当用户单击它时,他会得到一个包含两个txt文件的zip文件。 我很难编写完成此任务的控制器方法:-(有人能提供简短的示例吗? 非常感谢。这里有一些代码。代码是原始格式的,请根据您的使用情况进行增强。 我使用了DotNetZip和表存储numget 生成Zip的代码- usi
非常感谢。这里有一些代码。代码是原始格式的,请根据您的使用情况进行增强。 我使用了DotNetZip和表存储numget 生成Zip的代码-
using (ZipFile zip = new ZipFile())
using(MemoryStream stream = new MemoryStream())
{
BlobRepository rep = new BlobRepository();
// Download files from Blob storage
byte[] b = rep.DownloadFileFromBlob(filename);
zip.AddEntry("sample1", b);
//add as many files as you want
zip.Save(stream);
// use that stream for your usage.
}
下载blob的代码-
public byte[] DownloadFileFromBlob(string filename)
{
// Get Blob Container
CloudBlobContainer container = BlobUtilities.GetBlobClient.GetContainerReference(BlobUtilities.FileContainer);
// Get reference to blob (binary content)
CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);
// Read content
using (MemoryStream ms = new MemoryStream())
{
blockBlob.DownloadToStream(ms);
return ms.ToArray();
}
}
Blob实用工具辅助类-
internal class BlobUtilities
{
public static CloudBlobClient GetBlobClient
{
get
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("connection string here");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
return blobClient;
}
}
public static string FileContainer
{
get
{
return "container name here";
}
}
}
下面是一些代码。代码是原始格式的,请根据您的使用情况进行增强。 我使用了DotNetZip和表存储numget 生成Zip的代码-
using (ZipFile zip = new ZipFile())
using(MemoryStream stream = new MemoryStream())
{
BlobRepository rep = new BlobRepository();
// Download files from Blob storage
byte[] b = rep.DownloadFileFromBlob(filename);
zip.AddEntry("sample1", b);
//add as many files as you want
zip.Save(stream);
// use that stream for your usage.
}
下载blob的代码-
public byte[] DownloadFileFromBlob(string filename)
{
// Get Blob Container
CloudBlobContainer container = BlobUtilities.GetBlobClient.GetContainerReference(BlobUtilities.FileContainer);
// Get reference to blob (binary content)
CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);
// Read content
using (MemoryStream ms = new MemoryStream())
{
blockBlob.DownloadToStream(ms);
return ms.ToArray();
}
}
Blob实用工具辅助类-
internal class BlobUtilities
{
public static CloudBlobClient GetBlobClient
{
get
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("connection string here");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
return blobClient;
}
}
public static string FileContainer
{
get
{
return "container name here";
}
}
}