这是我对如何在 azure 中进行设置的简单描述。
http://geekswithblogs.net/MagnusKarlsson/archive/2012/12/02/how-to-use-azure-storage-for-images.aspx
//编辑; 这是我博客中的完整示例(如果博客死了)。yourViewName.cshtml
@model List<string>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<form action="@Url.Action("Upload")" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file1" />
<br />
<label for="file">Filename:</label>
<input type="file" name="file" id="file2" />
<br />
<label for="file">Filename:</label>
<input type="file" name="file" id="file3" />
<br />
<label for="file">Filename:</label>
<input type="file" name="file" id="file4" />
<br />
<input type="submit" value="Submit" />
</form>
@foreach (var item in Model) {
<img src="@item" alt="Alternate text"/>
}
你的控制器动作
public ActionResult Upload(IEnumerable<HttpPostedFileBase> file)
{
BlobHandler bh = new BlobHandler("containername");
bh.Upload(file);
var blobUris=bh.GetBlobs();
return RedirectToAction("Index",blobUris);
}
你的模型
public class BlobHandler
{
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
private string imageDirecoryUrl;
/// <summary>
/// Receives the users Id for where the pictures are and creates
/// a blob storage with that name if it does not exist.
/// </summary>
/// <param name="imageDirecoryUrl"></param>
public BlobHandler(string imageDirecoryUrl)
{
this.imageDirecoryUrl = imageDirecoryUrl;
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve a reference to a container.
CloudBlobContainer container = blobClient.GetContainerReference(imageDirecoryUrl);
// Create the container if it doesn't already exist.
container.CreateIfNotExists();
//Make available to everyone
container.SetPermissions(
new BlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
});
}
public void Upload(IEnumerable<HttpPostedFileBase> file)
{
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve a reference to a container.
CloudBlobContainer container = blobClient.GetContainerReference(imageDirecoryUrl);
if (file != null)
{
foreach (var f in file)
{
if (f != null)
{
CloudBlockBlob blockBlob = container.GetBlockBlobReference(f.FileName);
blockBlob.UploadFromStream(f.InputStream);
}
}
}
}
public List<string> GetBlobs()
{
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(imageDirecoryUrl);
List<string> blobs = new List<string>();
// Loop over blobs within the container and output the URI to each of them
foreach (var blobItem in container.ListBlobs())
blobs.Add(blobItem.Uri.ToString());
return blobs;
}
}