2017-08-26 24 views
0

私は、あなたがする必要がある今のコードダウンロード紺碧コンテナ

string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; 
     string[] arr = userName.Split('\\'); 
     string path = [email protected]"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([email protected]"{path}ImagesPath"); 
      using (var fileStream = System.IO.File.OpenWrite([email protected]"{path}\ImagesPath\{Name[2]}")) 
      { 
       blockBlob.DownloadToStream(fileStream); 
      } 

     } 
+0

動的ではどういう意味ですか?日付のベース?ユーザーの入力に基づいて?あなたがダウンロードしたときには、とにかくターゲットフォルダを変更することができます –

+0

ダウンロードのパスを選択するハードコードを作成しないでください –

+0

代わりにそこに入れたいですか?あなたは設定から​​ロードしますか?あなたはあなたがしたいことをあなたに話しただけです。 –

答えて

0

をzipファイルとして紺碧のコンテナ内のすべてのファイルをダウンロードすることがダイナミック これをダウンロードするには、パスを作りたいです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、zipファイルはコンテンツフォルダに生成されるため、ダウンロード操作用のURLを生成できます。

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

アクセスが拒否されました。 –

+0

"パス '〜/ Content/pictures20170828100934'へのアクセスが拒否されました。" –

+0

この例外を引き起こすコード行はどれですか?あなたの環境も提供してください。 AzureまたはあなたのローカルでWebアプリケーションを実行しましたか? – Amor

関連する問題