Asp.net mvc 将图片上载到Windows Azure网站
我有一个ASP.NET MVC 4应用程序要部署到Windows Azure。此应用程序的一部分涉及上传图片。上传图片时,我想将图片存储在位于Asp.net mvc 将图片上载到Windows Azure网站,asp.net-mvc,azure,Asp.net Mvc,Azure,我有一个ASP.NET MVC 4应用程序要部署到Windows Azure。此应用程序的一部分涉及上传图片。上传图片时,我想将图片存储在位于/pictures/upload的目录中 我的问题是,如何将图片上传到我在Microsoft Azure上托管的应用程序中的相对路径?到目前为止,我的应用程序已托管在虚拟机中。我可以通过以下方式完成上述工作: string path = ConfigurationManager.AppSettings["rootWebDirectory"] + "/pic
/pictures/upload
的目录中
我的问题是,如何将图片上传到我在Microsoft 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”]
指向一个绝对路径。我相信这就是我的问题所在。我不知道如何将所有这些切换到相对路径
谢谢大家! 数据和图像不应存储在网站目录中。这就是为什么会出现这种情况
string path = HostingEnvironment.MapPath(@"~/pictures/uploaded");
Azure的工作原理是将网站复制到一个实例,因此,如果存在多个实例,则上载的图像(存储在实例本地)将变得不同步,如果存在冲突,您甚至会丢失图像
如果你真的想这样做,下面的一行将为你提供你想要的:
string path = Server.MapPath("~/pictures/uploaded");
这里已经有一些很好的答案告诉你如何做你需要的事情。如果你不介意的话,请允许我提出一个替代方案 就个人而言,要解决像你这样的问题,我求助于。Blob存储是一种非常便宜和快速的存储二进制文件的方法(在文件夹类型的结构中),完全独立于云服务当前运行的VM 这将允许您在针对现有应用程序进行迁移或开发时获得一些额外的自由,因为您不必在每次部署时迁移上载。Blob存储文件也可以跨多个Azure数据中心进行三重复制,而无需额外成本,并且比部署VM具有更高的容错性 移动到Blob存储也将允许您在站点脱机或VM关闭时访问文件
您不再是在共享计算领域中操作,所有资源必须存在于同一台机器上。Azure是为可扩展性和资源分离而构建的。Blob存储是专门为您在这里尝试的操作而设计的。下面是我的简单介绍,介绍如何在azure中设置Blob存储。 //编辑;下面是我博客的完整例子(如果博客死了)。 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"/>
}
@型号列表
@{
ViewBag.Title=“Index”;
}
指数
文件名:
文件名:
文件名:
文件名:
@foreach(模型中的var项目){
}
您的控制器操作
public ActionResult Upload(IEnumerable<HttpPostedFileBase> file)
{
BlobHandler bh = new BlobHandler("containername");
bh.Upload(file);
var blobUris=bh.GetBlobs();
return RedirectToAction("Index",blobUris);
}
公共操作结果上传(IEnumerable文件)
{
BlobHandler bh=新BlobHandler(“容器名称”);
上传(文件);
var blobUris=bh.GetBlobs();
返回重定向操作(“索引”,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;
}
}
公共类BlobHandler
{
//从连接字符串中检索存储帐户。
CloudStorageAccount-storageAccount=CloudStorageAccount.Parse(
GetSetting(“StorageConnectionString”);
私有字符串imageDirecoryUrl;
///
///接收图片所在位置的用户Id并创建
///具有该名称的blob存储(如果不存在)。
///
///
公共BlobHandler(字符串imageDirecoryUrl)
{
this.imageDirecoryUrl=imageDirecoryUrl;
//创建blob客户端。
CloudBlobClient blobClient=storageAccount.CreateCloudBlobClient();
//检索对容器的引用。
CloudBlobContainer container=blobClient.GetContainerReference(imageDirecoryUrl);
//如果容器尚不存在,请创建该容器。
container.CreateIfNotExists();
//提供给每个人
container.SetPermissions(
新BlobContainerPermissions
{
PublicAccess=BlobContainerPublicAccessType.Blob
});
}
公共无效上载(IEnumerable文件)
{
//创建blob客户端。
CloudBlobClient blobClient=storageAccount.CreateCloudBlobClient();
//检索对容器的引用。
CloudBlobContainer container=blobClient.GetContainerReference(imageDirecoryUrl);
如果(文件!=null)
{
foreach(文件中的var f)
{
如果(f!=null)
{
CloudBlockBlob blockBlob=container.getblockblobbreference(f.FileName);
blockBlob.UploadFromStream(f.InputStream);
}
}
}
}
公共列表GetBlobs()
{
//创建blob客户端。
CloudBlobClient blobClient=storageAccount.CreateCloudBlobClient();
//检索对以前创建的容器的引用。
CloudBlobContainer container=blobClient.GetContainerReference(imageDirecoryUrl);
List BLOB=新列表();
//循环容器中的blob并将URI输出到每个blob
foreach(container.ListBlobs()中的var blobItem)
Add(blobItem.Uri.ToString());
返回斑点;
}
}
您能再解释一下吗?你是说Azure blob作为虚拟文件夹存在吗?我们可以使用Azure文件存储吗?blob和文件存储哪种方法最好?很有趣。开发环境如何?@SebastiánRojasR,不确定您的要求是什么,但有一个猜测是“我如何使用本地开发进行blob存储?”启动Azu中包含的Azure存储模拟器