使用Azure Blob存储列出容器的内容

时间:2016-03-29 17:24:40

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

我正在使用用C#编写的ASP.NET MVC创建云存储应用程序。我目前可以将文件上传到Azure存储blob容器,但每个文件名在上传页面本身上显示为“images”。 Azure中的文件名是正确的。

我想显示文件的实际名称而不是单词“images”。

Here is what it currently displays after a file is uploaded

我控制器的一段代码:

    public ActionResult Upload()
    {
        // Retrieve storage account from connection string.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            CloudConfigurationManager.GetSetting("AzureString"));

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

        CloudBlobContainer blobContainer = blobClient.GetContainerReference("Container");

        List<string> blobs = new List<string>();
        foreach (var blobItem in blobContainer.ListBlobs())
        {
            blobs.Add(blobItem.Uri.ToString());
        }
        return View(blobs);
    }

    [HttpPost]
    public ActionResult Upload(HttpPostedFileBase image)
    {
        if (image.ContentLength > 0)
        {
            // Retrieve storage account from connection string.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting("AzureString"));

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

            // Retrieve a reference to a container.
            CloudBlobContainer blobContainer = blobClient.GetContainerReference("Container");

            CloudBlockBlob blob = blobContainer.GetBlockBlobReference(image.FileName);
            blob.UploadFromStream(image.InputStream);
        }
        return RedirectToAction("Upload");
    }

相关观点:

    @{
        ViewBag.Title = "Upload";
    }

    @if (Request.IsAuthenticated)
    {
        <h2>Upload</h2>
        <p>
            @using (Html.BeginForm("Upload", "Files", FormMethod.Post, new { enctype = "multipart/form-data" }))
            {
                <input type="file" name="image" />
                <input type="submit" value="Upload" />
            }
        </p>
        <ul style="list-style-type: none; padding:0">
            @foreach (var item in Model)
            {
                <li>
                    <img src="@item" alt="images" width="100" height="100" />
                    <a id="@item" href="#" onclick="deleteImage('@item');">Delete</a>
                </li>
            }
        </ul>

        <script>
        function deleteImage(item)
        {
            var url = "/Files/DeleteImage";
            $.post(url, {Name: item}, function (data) {
            window.location.href = "/Files/Upload";
            alert(data);
            })
        }
        </script>
    }
    else
    {
        <ul class="nav navbar-nav navbar-right">
            <li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
            <li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
        </ul>
    }

如果这是一个简单的错误,请提前道歉。我是ASP.NET,C#和Azure的新手。

1 个答案:

答案 0 :(得分:0)

能够通过使容器公开显示来实现它的工作

<script src="script/jquery-2.2.1.min.js"></script>