将图片上传到 Windows Azure 网站 [英] Upload Picture to Windows Azure Web Site

查看:34
本文介绍了将图片上传到 Windows Azure 网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的问题是,如何将图片上传到托管在 Windows Azure 上的应用程序中的相对路径?到目前为止,我的应用程序已托管在虚拟机中.我可以使用以下方法完成上述操作:

string path = ConfigurationManager.AppSettings["rootWebDirectory"] + "/pictures/uploaded;//获取文件路径if (Directory.Exists(path) == false)Directory.CreateDirectory(路径);string filePath = path + "/uploaded" + DateTime.UtcNow.Milliseconds + ".png";filePath = filePath.Replace("/", "\").Replace("\\", "\");//将图片写入文件系统byte[] bytes = GetPictureBytes();使用 (FileStream fileStream = new FileStream(filePath, FileMode.Create)){fileStream.Write(bytes, 0, bytes.Length);fileStream.Flush();fileStream.Close();}

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

谢谢!

解决方案

数据和图像不应存储在网站目录中.这就是 Azure Blob 存储的原因.>

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

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

string path = Server.MapPath("~/pictures/uploaded");

I have an ASP.NET MVC 4 app that i want to deploy to Windows Azure. A part of this app involves uploading a picture. When the picture is uploaded, I want to store the picture in a directory located at /pictures/uploaded.

My question is, how do I upload a picture to a relative path within my app hosted on Windows Azure? Up to this point, my app has been hosted in a Virtual Machine. I was able to do the above by using the following:

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();
}

Currently, ConfigurationManager.AppSettings["rootWebDirectory"] points to an absolute path. I belive this is where my problem lies. I can't figure out how to switch all of this to a relative path.

Thank you!

解决方案

Data and images are not meant to be stored in website directory. That's why there is the Azure Blob Storage.

Azure works by copying the website out to an instance, so if more than one instance exists then uploaded images (which are stored local to the instance) will become out of sync, you will even loose images if there are conflicts.

If you really want to do it the line below will give you what you want:

string path = Server.MapPath("~/pictures/uploaded");

这篇关于将图片上传到 Windows Azure 网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆