【问题标题】:Windows Azure blob zip and send to client (ASP.NET MVC)Windows Azure blob zip 并发送到客户端 (ASP.NET MVC)
【发布时间】:2015-11-05 08:04:11
【问题描述】:

我有一些文件存储在 Windows Azure blob 容器中(比如说 file1.txt 和 file2.txt)。 在 ASP.NET MVC4 应用程序(托管在 azurewebsites 上)中,我需要创建一个“以 zip 格式下载”链接。当用户单击它时,他会得到一个包含两个 txt 文件的 zip 文件。 我很难编写控制器方法来完成这项任务:-(有人可以提供简短的例子吗? 非常感谢。

【问题讨论】:

    标签: asp.net-mvc-4 azure zip azure-blob-storage


    【解决方案1】:

    这里有一些代码。代码为原始格式,请根据您的使用情况对其进行增强。 我使用了 DotNetZip 和 Table Storage Nugets。

    生成 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";
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-06-12
      • 1970-01-01
      • 2015-03-13
      • 2014-11-22
      • 1970-01-01
      • 1970-01-01
      • 2011-02-23
      • 1970-01-01
      • 2011-03-10
      相关资源
      最近更新 更多