public ActionResult SetMetadata(int blocksCount, string fileName, long fileSize, int fileIndex)
 {
     var container = CloudStorageAccount.Parse(
         ConfigurationManager.AppSettings["StorageConnectionString"]).CreateCloudBlobClient()
         .GetContainerReference(ConfigurationManager.AppSettings["CloudStorageContainerReference"]);
     container.CreateIfNotExists();
     var fileToUpload = new CloudFile()
     {
         BlockCount = blocksCount,
         FileName = fileName,
         Size = fileSize,
         BlockBlob = container.GetBlockBlobReference(fileName),
         StartTime = DateTime.Now,
         IsUploadCompleted = false,
         UploadStatusMessage = string.Empty,
         FileKey = "CurrentFile" + fileIndex.ToString(),
         FileIndex = fileIndex
     };
     Session.Add(fileToUpload.FileKey, fileToUpload);
     return Json(new { success = true, index = fileIndex });
 }
 private ActionResult CommitAllChunks(CloudFile model)
 {
     model.IsUploadCompleted = true;
     bool errorInOperation = false;
     try
     {
         var blockList = Enumerable.Range(1, (int)model.BlockCount).ToList<int>().ConvertAll(rangeElement =>
                     Convert.ToBase64String(Encoding.UTF8.GetBytes(
                         string.Format(CultureInfo.InvariantCulture, "{0:D4}", rangeElement))));
         model.BlockBlob.PutBlockList(blockList);
         var duration = DateTime.Now - model.StartTime;
         float fileSizeInKb = model.Size / 1024;
         string fileSizeMessage = fileSizeInKb > 1024 ?
             string.Concat((fileSizeInKb / 1024).ToString(CultureInfo.CurrentCulture), " MB") :
             string.Concat(fileSizeInKb.ToString(CultureInfo.CurrentCulture), " KB");
         model.UploadStatusMessage = string.Format(CultureInfo.CurrentCulture,
             "File uploaded successfully. {0} took {1} seconds to upload",
             fileSizeMessage, duration.TotalSeconds);
     }
     catch (StorageException e)
     {
         model.UploadStatusMessage = "Failed to Upload file. Exception - " + e.Message;
         errorInOperation = true;
     }
     finally
     {
         Session.Remove(model.FileKey);
     }
     return Json(new
     {
         error = errorInOperation,
         isLastBlock = model.IsUploadCompleted,
         message = model.UploadStatusMessage,
         index = model.FileIndex
     });
 }
 private JsonResult UploadCurrentChunk(CloudFile model, byte[] chunk, int id)
 {
     using (var chunkStream = new MemoryStream(chunk))
     {
         var blockId = Convert.ToBase64String(Encoding.UTF8.GetBytes(
                 string.Format(CultureInfo.InvariantCulture, "{0:D4}", id)));
         try
         {
             model.BlockBlob.PutBlock(
                 blockId,
                 chunkStream, null, null,
                 new BlobRequestOptions()
                 {
                     RetryPolicy = new LinearRetry(TimeSpan.FromSeconds(10), 3)
                 },
                 null);
             return null;
         }
         catch (StorageException e)
         {
             Session.Remove(model.FileKey);
             model.IsUploadCompleted = true;
             model.UploadStatusMessage = "Failed to Upload file. Exception - " + e.Message;
             return Json(new { error = true, isLastBlock = false, message = model.UploadStatusMessage, index = model.FileIndex });
         }
     }
 }