在ASP.NET MVC中通过复选框从Azure Blob存储下载多个文件

时间:2019-05-03 11:41:59

标签: c# asp.net-mvc azure azure-blob-storage

我是构建控制器,用于列出blob并点击下载。

我试图更改View文件,但无法获得想要的结果。

Homecontroller中的代码

public ActionResult Get()
        {
            // Container Name - picture
            string v1 = "jpp";
            string u1 = User.Identity.Name.ToLower();
            u1 = u1.Replace(' ', '-');

            //IEnumerable<Claim> claims = ClaimsPrincipal.Current.Claims;

            BlobManager BlobManagerObj = new BlobManager (u1);

            List<string> fileList = BlobManagerObj.BlobList();

            return View(fileList);
        }
    [Authorize]
    public ActionResult ListBlobs()
    {
        CloudBlobContainer container = GetCloudBlobContainer();
        List<string> blobs = new List<string>();
        foreach (IListBlobItem item in container.ListBlobs(useFlatBlobListing: true))
        {
            if (item.GetType() == typeof(CloudBlockBlob))
            {
                CloudBlockBlob blob = (CloudBlockBlob)item;
                blobs.Add(blob.Name);
            }
            else if (item.GetType() == typeof(CloudPageBlob))
            {
                CloudPageBlob blob = (CloudPageBlob)item;
                blobs.Add(blob.Name);
            }
            else if (item.GetType() == typeof(CloudBlobDirectory))
            {
                CloudBlobDirectory dir = (CloudBlobDirectory)item;
                blobs.Add(dir.Uri.ToString());
            }
        }

        return View(blobs);
    }

    private CloudBlobContainer GetCloudBlobContainer()
    {
        string u2 = User.Identity.Name.ToLower();
        u2 = u2.Replace(' ', '-');

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=stellarappstorage;AccountKey=M9cAqD+Wo4x3FqY/t8ebItjfl3sxZGVinkTwEXiw/ANkD3jVqKEMdYWo5BWveN6WtSjtfxX+G2FseeLDkDtkOw==;EndpointSuffix=core.windows.net");
        CloudBlobClient BlobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer c1 = BlobClient.GetContainerReference(u2);

        return c1;

    }

    public ActionResult GetFileFromBlob(string id)
    {

        MemoryStream ms = new MemoryStream();

        string u2 = User.Identity.Name.ToLower();
        u2 = u2.Replace(' ', '-');

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=stellarappstorage;AccountKey=***********/ANkD3jVqKEMdYWo5BWveN6WtSjtfxX+G2FseeLDkDtkOw==;EndpointSuffix=core.windows.net");

        CloudBlobClient BlobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer c1 = BlobClient.GetContainerReference(u2);

        if (c1.Exists())
        {
            CloudBlob file = c1.GetBlobReference(id);

            if (file.Exists())
            {
                file.DownloadToStreamAsync(ms);
                Stream blobStream = file.OpenReadAsync().Result;
                return File(blobStream, file.Properties.ContentType, file.Name);
            }
            else
            {
                return Content("File does not exist");
            }
        }
        else
        {
            return Content("Dir does not exist");
        }
    }






*** code of view file(ListBlob)***
 @{
        ViewBag.Title = "ListBlobs";
    }

    @model List<string>
    <h2>List blobs</h2>
    <ul>
        @foreach (var item in Model)
        {
            <li>  <a href="/home/GetFileFromBlob/?id=@item">@item</a></li>
        }

    </ul>

输出的屏幕截图 enter image description here

我要做的是: 为文件添加复选框选项,以允许用户下载多个文件。 仅显示文件夹,而不显示文件夹中的文件,并使文件夹可单击以查看文件和文件夹

0 个答案:

没有答案