2017-08-26 78 views
0

我想下載Azure上的zip文件容器中的所有文件,使路徑下載是動態 這個現在代碼下載蔚藍的容器作爲zip文件asp.net的MVC

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

您需要使用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