13

我有一个想要部署到 Windows Azure 的 ASP.NET MVC 4 应用程序。这个应用程序的一部分涉及上传图片。图片上传后,我想将图片存储在位于/pictures/uploaded.

我的问题是,如何将图片上传到托管在 Windows Azure 上的应用程序中的相对路径?到目前为止,我的应用程序已托管在虚拟机中。我能够通过使用以下内容来完成上述操作:

string path = ConfigurationManager.AppSettings["rootWebDirectory"] + "/pictures/uploaded;

// Get the file path
if (Directory.Exists(path) == false)
  Directory.CreateDirectory(path);

string filePath = path + "/uploaded" + DateTime.UtcNow.Milliseconds + ".png";
filePath = filePath.Replace("/", "\\").Replace("\\\\", "\\");

// Write the picture to the file system
byte[] bytes = GetPictureBytes();
using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
{
  fileStream.Write(bytes, 0, bytes.Length);
  fileStream.Flush();
  fileStream.Close();
}

当前,ConfigurationManager.AppSettings["rootWebDirectory"]指向绝对路径。我相信这就是我的问题所在。我不知道如何将所有这些切换到相对路径。

谢谢!

4

4 回答 4

21

这是我对如何在 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;
         }
     }
于 2013-01-15T21:37:09.587 回答
10

数据和图像并不意味着存储在网站目录中。这就是为什么有Azure Blob 存储

Azure 通过将网站复制到一个实例来工作,因此如果存在多个实例,则上传的图像(存储在实例本地)将变得不同步,如果存在冲突,您甚至会丢失图像。

如果你真的想这样做,下面的行会给你你想要的:

string path = Server.MapPath("~/pictures/uploaded");
于 2013-01-14T17:45:12.503 回答
3

这里已经有一些很好的答案可以准确地告诉您如何做您需要的事情。如果您不介意,请允许我提出一个替代方案。

就个人而言,为了解决像您这样的问题,我求助于Azure Blob 存储。Blob 存储是一种非常便宜且快速的存储二进制文件(在文件夹类型结构中)的方法,与您的云服务当前运行的 VM 完全分离。

这将允许您在针对现有应用程序迁移或开发时获得一些额外的自由,因为您不必在每次部署时迁移您的上传。Blob 存储文件还可以跨多个 Azure 数据中心进行三重复制,无需额外费用,并且比部署 VM 更容错。

迁移到 Blob 存储还允许您在站点离线或 VM 关闭时访问文件。

您不再在所有资源必须存在于同一台机器上的共享计算领域中运行。Azure 专为资源的可伸缩性和分离而构建。Blob 存储专为您在此处尝试执行的操作而设计。

于 2013-01-14T19:01:23.417 回答
-1
string path = HostingEnvironment.MapPath(@"~/pictures/uploaded");
于 2013-01-14T17:38:20.383 回答