Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正在尝试链接到cshtml页面上的azure blob文件_C#_Asp.net Mvc 4_Azure_Razor_Azure Storage Blobs - Fatal编程技术网

C# 正在尝试链接到cshtml页面上的azure blob文件

C# 正在尝试链接到cshtml页面上的azure blob文件,c#,asp.net-mvc-4,azure,razor,azure-storage-blobs,C#,Asp.net Mvc 4,Azure,Razor,Azure Storage Blobs,我已经上传了一些jpg和txt文件到azure blob商店,我已经阅读了,所以我知道如何检索它们 我想弄清楚的是,当我的cshtml页面中的链接被点击时,如何加载和链接到文件 谢谢 如果您知道如何检索文件,您知道如何加载文件,您可以设置如下非常简单的设置 表示要在视图/页面上显示的数据的ViewModel。 public class FileViewModel { public string FileName {get; set;} public string AzureUrl {

我已经上传了一些jpg和txt文件到azure blob商店,我已经阅读了,所以我知道如何检索它们

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


谢谢

如果您知道如何检索文件,您知道如何加载文件,您可以设置如下非常简单的设置

表示要在视图/页面上显示的数据的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)
}
public ActionResult列表文件()
{
var fileList=新列表();
//…连接到azure帐户和容器的代码
foreach(容器中的IListBlobItem项。ListBlobs(null,false))
{
if(item.GetType()==typeof(CloudBlockBlob))
{
CloudBlockBlob blob=(CloudBlockBlob)项;
//如果blob容器的ACL是私有的,则不能通过简单的URL访问blob
//创建共享访问签名(SAS)令牌,该令牌提供对私有资源的时间/权限限制访问。
var sasToken=blob.GetSharedAccessSignature(新的SharedAccessBlobPolicy()
{
权限=SharedAccessBlobPermissions.Read,
SharedAccessExpiryTime=DateTime.UtcNow.AddHours(1),//假设用户在页面上停留一小时。
});
var blobUrl=blob.Uri.AbsoluteUri+sasToken;//这将确保用户能够访问blob一小时。
添加(新的FileViewModel)
{
FileName=blob.Name,
AzureUrl=blobUrl
});
}
}
返回视图(文件列表)
}
cshtml视图

@model IEnumerable<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>
}
@model IEnumerable
文件列表
@foreach(模型中的var文件)
{
//链接将在新选项卡中打开
}

只有当Blob的容器是公共的时,这才起作用。在本文中,我们将解释如何创建和使用私有Blob容器。感谢GauravMantri指出了这一点。

当用户在.cshtml@Overmachine中单击一个链接时,您想下载一个文件吗,虽然最好在“新建”选项卡中打开文件。@viperguynaz我真的不知道如何以可在网页中访问的方式存储文件,因此我不确定要尝试什么。如果blob容器的ACL是私有的,会发生什么?最好给出一个具有读取权限的SAS URL,而不是blob URI。@GauravMantri是的,只有当容器是公共的,基于提供的链接@GauravMantri时,这才有效。一点也不,试试看