2014-03-31 38 views
0

我刚刚将我的c#web应用程序部署到了Windows Azure。图片上传在我的本地机器上正常工作,但是现在网站已经部署了,图片上传不能和Azure存储一起使用。我刚收到一条错误消息,说Error. An error occurred while processing your request.尝试上载图像时。图片上传无法在Azure存储上工作

任何帮助将不胜感激。

图片上传控制器

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> 
} 
+1

请指出您是将这部署到Azure网站,Azure虚拟机还是Azure云服务。 – MikeWo

+0

我正在部署到Azure云服务 – Dolaps

+0

您是否有写入磁盘的权限?你不应该将这些文件存储在blob存储中吗? –

回答

0

正如阿兰在评论上述建议,我也建议你存储你的图像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); 

    }