2014-03-24 29 views

回答

3

首先在Azure中创建容器,并将您的AccountName和Access Key移动到您的blob存储中。

请点击此链接: http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs-20/

比你的视觉工作室创建一个类文件,并按照下面的代码给出:

public class BlobStorageService 
{ 
    public CloudBlobContainer GetCloudBlobContainer() 
    { 
     CloudStorageAccount storageAccount = CloudStorageAccount.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["BlobSetting"]); 
     CloudBlobClient blobclient = storageAccount.CreateCloudBlobClient(); 
     CloudBlobContainer blobcontainer = blobclient.GetContainerReference("mycontainer");    
     if (blobcontainer.CreateIfNotExists()) 
     { 
      blobcontainer.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }); 
     } 
     return blobcontainer; 
    } 

    public string GetReadData(string filename) 
    { 
     // Retrieve storage account from connection string. 
     CloudStorageAccount storageAccount = CloudStorageAccount.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["BlobSetting"]); 

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

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

     // Retrieve reference to a blob named "myblob.csv" 
     CloudBlockBlob blockBlob2 = container.GetBlockBlobReference(filename); 

     string text; 
     using (var memoryStream = new MemoryStream()) 
     { 
      blockBlob2.DownloadToStream(memoryStream); 
      text = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray()); 
     } 

     return text; 
    } 
} 

控制器:

[HttpPost] 
public ActionResult UploadDevicesToRegister11(HttpPostedFileBase userDetailCsvfile) 
{ 
    BlobStorageService df = new BlobStorageService(); 

    if (userDetailCsvfile.ContentLength > 0) 
    { 
     //To upload file on Blob 
     CloudBlobContainer blobContainer = df.GetCloudBlobContainer(); 
     CloudBlockBlob blob = blobContainer.GetBlockBlobReference(userDetailCsvfile.FileName); 
     blob.UploadFromStream(userDetailCsvfile.InputStream); 

     //To read File from Blob 
     blobContainerRead = df.GetReadData(userDetailCsvfile.FileName); 
    } 

    return View(); 
} 

希望这有助于。

+0

感谢您的详细解释兄弟......我明白了......我对容器有问题...... –

相关问题