2014-07-17 39 views
1

我已经上传了一些jpg和txt文件到天蓝色的blob商店,我读this tutorial,所以我知道如何检索它们。尝试链接到cshtml页面上的天蓝色blob文件

我想弄清楚的是如何加载和链接到我的cshtml页面中的链接点击时的文件。

谢谢!

+0

当用户单击.cshtml中的链接时,是否要下载文件? – Overmachine

+0

@Overmachine这将是好的,虽然在新选项卡中打开的文件将是可取的。 – Slicedbread

+0

@viperguynaz我真的不知道如何以一种可以在网页上访问的方式存储文件,所以我不知道该如何尝试。 – Slicedbread

回答

6

如果你知道如何检索文件,你知道如何加载它们,你可以设置一些非常简单的东西。

这个ViewModel将代表你想要在视图/页面上显示的数据。

public class FileViewModel 
{ 

    public string FileName {get; set;} 
    public string AzureUrl {get; set;} 

} 

控制器动作

public ActionResult ListFiles() 
{ 

var fileList = new List<FileViewModel>(); 

//.. code to connect to the azure account and container 

foreach (IListBlobItem item in container.ListBlobs(null, false)) 
{ 
     if (item.GetType() == typeof(CloudBlockBlob)) 
     { 
      CloudBlockBlob blob = (CloudBlockBlob)item; 
     //In case blob container's ACL is private, the blob can't be accessed via simple URL. For that we need to 
     //create a Shared Access Signature (SAS) token which gives time/permission bound access to private resources. 
     var sasToken = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy() 
     { 
      Permissions = SharedAccessBlobPermissions.Read, 
      SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1),//Asssuming user stays on the page for an hour. 
     }); 
     var blobUrl = blob.Uri.AbsoluteUri + sasToken;//This will ensure that user will be able to access the blob for one hour. 

      fileList.Add(new FileViewModel 
      { 
       FileName = blob.Name, 
       AzureUrl = blobUrl 
      }); 

     } 
    } 
return View(fileList) 
} 

的CSHTML视图

@model FileViewModel 


<h2>File List</h2> 

@foreach(var file in Model) 
{ 
    //link will be opened in a new tab 
    <a target="_blank" href="@file.AzureUrl">@file.FileName</a> 
} 

这如果斑的容器是在这个link公众解释了如何创建只会工作,使用私人blob容器。感谢GauravMantri谁指出了。

+1

如果blob容器的ACL是私有的,会发生什么情况?最好给出一个具有读取权限而不是blob URI的SAS网址。 –

+0

@GauravMantri是的,这只会在容器公开时才起作用,基于提供的链接http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/ – Overmachine

+1

你介意我是否更新了包含SAS URL的答案? –

相关问题