Image upload not working on Azure Storage

I have just deployed my c# web application to windows azure. Image upload works fine on my local machine however now the website has been deployed the image upload doesn't work with azure storage. I just recieve an error saying Error. An error occurred while processing your request. When trying to upload an image.

Any help would be grateful.

Image Upload Controller

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

** Image Upload 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>
}

Answers


As Aran suggested in the comments above, I too would recommend that you store your images in blob storage. Storing images on the hard drive should only be done for static images, which would be bundled and deployed with your solution. You cannot count on the images being present if you instance of your site is moved to another server. Storing images in SqlAzure tables or blobs will also enable you to better scale your application should you wish to increase the number of website instances you employ for you solution

Below is an example code snippet I use which gives you an idea of how to use the storage client to write to 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);

    }

Need Your Help

Date-based archives for custom post_types (wordpress 3.0)

wordpress date permalinks custom-post-type

If I have a custom post type named 'my_type', how can I get Wordpress to make date-based archives, for example: