映像上传在 Azure 存储上不起作用
本文关键字:存储 不起作用 Azure 映像 | 更新日期: 2023-09-27 17:55:48
我刚刚将我的 c# Web 应用程序部署到 Windows Azure。图像上传在我的本地计算机上工作正常,但是现在网站已部署,图像上传不适用于 Azure 存储。我只是收到一条错误消息,指出Error. An error occurred while processing your request.
尝试上传图像时。
任何帮助将不胜感激。
图片上传控制器
public ActionResult Create(UserprofileImage userprofileimage, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
if (file != null)
{
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var currentUser = manager.FindById(User.Identity.GetUserId());
file.SaveAs(HttpContext.Server.MapPath("~/Images/")
+ file.FileName);
userprofileimage.userImagePath = file.FileName;
userprofileimage.UserId = currentUser.Id;
userprofileimage.current = 1;
db.UserprofileImages.Add(userprofileimage);
db.SaveChanges();
var userimage = db.UserprofileImages.Where(u => u.UserId == currentUser.Id && u.Id != userprofileimage.Id).ToList();
foreach(var item in userimage)
{
item.Id = 0;
db.SaveChanges();
}
return RedirectToAction("Index", "Profile");
}
}
return View(userprofileimage);
}
** 图片上传 HTML **
@using (Html.BeginForm("Create", "UserprofileImage", null, FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
<div class="control-label col-md-2">
Profile Image
</div>
<div class="col-md-10">
<input id="userImagePath" title="Upload a profile picture"
type="file" name="file" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
正如 Aran 在上面的评论中所建议的那样,我也建议您将图像存储在 blob 存储中。在硬盘驱动器上存储映像应仅针对静态映像执行,这些静态映像将与解决方案捆绑和部署。如果将站点的实例移动到另一台服务器,则无法指望存在的图像。在 SqlAzure 表或 blob 中存储图像还可以使你能够更好地缩放应用程序,如果你希望增加为解决方案使用的网站实例数
下面是我使用的示例代码片段,它让你了解如何使用存储客户端写入 Blob。
public async Task AddPhotoAsync(Photo photo)
{
var containerName = string.Format("profilepics-{0}", photo.ProfileId).ToLowerInvariant();
var blobStorage = _storageService.StorageAccount.CreateCloudBlobClient();
var cloudContainer = blobStorage.GetContainerReference("profilephotos");
if(cloudContainer.CreateIfNotExists())
{
var permissions = await cloudContainer.GetPermissionsAsync();
permissions.PublicAccess = BlobContainerPublicAccessType.Container;
cloudContainer.SetPermissions(permissions);
}
string uniqueBlobName = string.Format("profilephotos/image_{0}{1}", Guid.NewGuid().ToString(),Path.GetExtension(photo.UploadedImage.FileName));
var blob = cloudContainer.GetBlockBlobReference(uniqueBlobName);
blob.Properties.ContentType = photo.UploadedImage.ContentType;
blob.UploadFromStream(photo.UploadedImage.InputStream);
photo.Url = blob.Uri.ToString();
await AddPhotoToDB(photo);
}