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

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

问题描述

我有一个ASP.NET MVC应用程序4,我要部署到Windows Azure。这个应用程序的一部分包括上传图片。当上传了图片,我要的图片存储在位于目录/图片/上传

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.

我的问题是,我怎么上传图片托管在Windows Azure上我的应用程序中的相对路径?到现在为止,我的应用程序已经被托管的虚拟机。我可以通过使用以下上面做:

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

目前, ConfigurationManager.AppSettings [rootWebDirectory] 指向一个绝对路径。我相信这是我的问题所在。我无法弄清楚如何这一切切换到一个相对路径。

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.

感谢您!

推荐答案

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

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

Azure的作品通过复制网站到一个实例,因此,如果多个实例存在,那么上传的图片(保存本地实例),将成为不同步,你甚至会松动的图像是否有冲突。

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天全站免登陆