我刚刚将我的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>
}
答案 0 :(得分:0)
正如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);
}