我将此项目部署在 azure 网站上(在一个实例上运行),该网站获取不同的字段(姓名、年龄……),并且还允许上传图像。然后所有数据都存储在天蓝色数据库中。
问题是我无法保存上传的文件。它在本地工作正常,但部署时出现“找不到路径 'D:.... 的一部分”错误。
我已尝试将文件保存在 Temp 和 localresources 中,但我一定缺少某些内容或实施不正确,我也无权访问 blob 存储。
如何正确保存上传的文件(我想存储在数据库中)?
这就是我尝试保存图像的方式和位置:
[HttpPost]
public ActionResult Create(HttpPostedFileBase file, [Bind(Include = "StudentId,Name,Age")] student13 student13)
{
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
这是视图:
<form action="" method="post" enctype="multipart/form-data">
<label for="file">picture:</label>
<input type="file" name="file" id="file" class="form-control" />
<input type="submit" class="btn btn-default" value="Create" />
</form>
可以在Azure网站的文件系统上进行写入,但是您的写入权限仅限于应用程序的根文件夹。您可以在
Server.MapPath("~/from_here_on")
内的任何位置书写,请查看this了解详细信息。
您可以参考如何将Azure Blob存储与Azure网站一起使用将您的网站与azure存储连接并将图像保存到存储中。这里是有关 Azure 存储的最新文档使用 .NET 开始使用 Azure Blob 存储。希望它能有所帮助。
[HttpPost]
public ActionResult ImageUpload()
{
var image = Request.Files["image"];
if (image == null)
{
ViewBag.UploadMessage = "Failed to upload image";
}
else
{
ViewBag.UploadMessage = String.Format("Got image {0} of type {1} and size {2}",
image.FileName, image.ContentType, image.ContentLength);
// TODO: actually save the image to Azure blob storage
}
return View();
}