使用 Azure Blob 存储列出容器的内容
Listing the contents of a container with Azure Blob storage
我正在使用用 C# 编写的 ASP.NET MVC 创建云存储应用程序。我目前可以将文件上传到 Azure 存储 blob 容器,但每个文件名在上传页面上都显示为单词 "images"。文件名在 Azure 中是正确的。
我想显示文件的实际名称而不是单词,"images"。
来自我的控制器的一段代码:
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 的新手。
能够通过使容器公开可见来使其正常工作
container.SetPermissions(
new BlobContainerPermissions
{ PublicAccess = BlobContainerPublicAccessType.Blob});
我正在使用用 C# 编写的 ASP.NET MVC 创建云存储应用程序。我目前可以将文件上传到 Azure 存储 blob 容器,但每个文件名在上传页面上都显示为单词 "images"。文件名在 Azure 中是正确的。
我想显示文件的实际名称而不是单词,"images"。
来自我的控制器的一段代码:
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 的新手。
能够通过使容器公开可见来使其正常工作
container.SetPermissions(
new BlobContainerPermissions
{ PublicAccess = BlobContainerPublicAccessType.Blob});