【问题标题】:download azure container as zip file asp.net mvc将 azure 容器下载为 zip 文件 asp.net mvc
【发布时间】:2017-08-26 23:31:19
【问题描述】:

我想将 azure 上容器中的所有文件下载为 zip 文件,并使下载路径为动态 这是现在的代码

string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
        string[] arr = userName.Split('\\');
        string path = $@"C:\Users\{arr[1]}\Downloads\";
        CloudBlobContainer contianner = BlobClient.GetContainerReference(contianerName);

        var list = contianner.ListBlobs();

       /// Console.WriteLine(list.Count());
        string[] FilesName = new string[list.Count()];
        int i = 0;
        foreach (var blob in list)
        {
            string[] Name = blob.Uri.AbsolutePath.Split('/');
            FilesName[i++] = Name[2];
          //  Console.WriteLine(Name[2]);
            CloudBlockBlob blockBlob = contianner.GetBlockBlobReference(Name[2]);
            System.IO.Directory.CreateDirectory($@"{path}ImagesPath");
            using (var fileStream = System.IO.File.OpenWrite($@"{path}\ImagesPath\{Name[2]}"))
            {
                blockBlob.DownloadToStream(fileStream);
            }

        }

【问题讨论】:

  • 动态是什么意思?基于日期?基于用户输入?照原样,当您下载时,用户仍然可以更改目标文件夹
  • 不要硬编码选择下载路径
  • 那么你想在里面放什么?你想从配置加载吗?你只告诉了你不想想做的事。
  • 需要用户选择下载路径或者强制浏览器下载不想在代码中写路径
  • 现在您终于解释了您的要求:这是一个基于浏览器的功能,您无法覆盖(您是否曾经在浏览器中下载过某些内容,并且显示了一个要求路径的弹出窗口?)您可以使用客户端提示预先接受路径,然后将其作为参数发送到此方法。但是您发布的方法缺少所有允许我们对此发表评论的网络特定代码。这是无法从浏览器提示用户的服务器端代码。您需要使用客户端代码 (javascript) 执行此操作,然后将结果传递给此方法。

标签: c# asp.net-mvc azure


【解决方案1】:

您需要使用 3 个步骤来完成您的工作。

第 1 步,将所有文件下载到一个文件夹中。我建议您在 Web 应用程序的内容文件夹下创建一个文件夹。

// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("storage connection string");

// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

string containerName = "mycontainer";

// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(containerName);

var blobs = container.ListBlobs(string.Empty, true);
string currentDateTime = DateTime.Now.ToString("yyyyMMddhhmmss");
string directoryPath = Server.MapPath("~/Content/" + containerName + currentDateTime);
System.IO.Directory.CreateDirectory(directoryPath);
foreach (CloudBlockBlob blockBlob in blobs)
{
    string[] segements = blockBlob.Name.Split('/');
    string subFolderPath = directoryPath;
    for (int i = 0; i < segements.Length - 1; i++)
    {
        subFolderPath = subFolderPath + "\\" + segements[i];
        if (!System.IO.Directory.Exists(subFolderPath))
        {
            System.IO.Directory.CreateDirectory(subFolderPath);
        }
    }
    string filePath = directoryPath + "\\" + blockBlob.Name;
    blockBlob.DownloadToFile(filePath, System.IO.FileMode.CreateNew);
}

Console.WriteLine("Download files successful.");

第 2 步,下载文件后,我们可以使用 System.IO.Compression.ZipFile 类压缩文件夹。要使用它,我们需要添加对 2 个程序集的引用。 System.IO.Compression 和 System.IO.Compression.FileSystem。

System.IO.Compression.ZipFile.CreateFromDirectory(directoryPath, directoryPath + ".zip");
Console.WriteLine("Compress the folder successfully");

第3步,由于压缩文件是在内容文件夹中生成的,因此您可以生成下载操作的URL。

string url = "http://hostname:port/content/" + containerName + currentDateTime + ".zip"; 

【讨论】:

  • 访问下载路径被拒绝访问
  • "访问路径 '~/Content/pictures20170828100934' 被拒绝。"
  • 哪一行代码导致了这个异常?还请提供您的环境。您是在 Azure 还是本地运行 Web 应用程序?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多