public string UploadAttachment(FormCollection coll) { try { System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12; DataControl.CurrentInfo objCur = new DataControl.CurrentInfo(); string companyCode = objCur.GetCompanyCode(); string newFilename = string.Empty; var container = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse( ConfigurationManager.AppSettings["NBBLOBACCKEY"]).CreateCloudBlobClient() .GetContainerReference(companyCode.ToLower()); container.CreateIfNotExists(); //int blocksCount = int.Parse(coll["blocksCount"].ToString()); //string fileName = coll["fileName"].ToString(); //long fileSize = long.Parse(coll["fileSize"].ToString()); int blocksCount = int.Parse(coll["blocksCount"].ToString()); string fileName = coll["fileName"].ToString(); fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1); newFilename = fileName; string fileExtenstion = fileName.Substring(fileName.LastIndexOf(".")); long fileSize = long.Parse(coll["fileSize"].ToString()); newFilename = newFilename.Substring(0, newFilename.LastIndexOf('.')); newFilename = newFilename.ToString().Trim().Replace(" ", "_"); //Special Character are replaced. newFilename = Regex.Replace(newFilename, @"[^0-9a-zA-Z_]+", "_"); fileName = newFilename + "_" + objCur.GetUserName() + "_" + DateTime.Now.ToString("ddMMyyyyHHmmssfff") + fileExtenstion; var fileToUpload = new MVCModels.CloudFile() { BlockCount = blocksCount, FileName = fileName, Size = fileSize, BlockBlob = container.GetBlockBlobReference(fileName), StartTime = DateTime.Now, IsUploadCompleted = false, UploadStatusMessage = string.Empty }; Session.Add("CurrentFile", fileToUpload); return(fileName); } catch (Exception ex) { Dictionary <string, string> dicContext = new Dictionary <string, string>(); dicContext.Add("Method:", MethodBase.GetCurrentMethod().Name); DataControl.Impl.ExceptionHandler.WriteLog(ex, dicContext); throw ex; } }
public ActionResult UploadChunk(int id) { try { HttpPostedFileBase request = Request.Files["Slice"]; byte[] chunk = new byte[request.ContentLength]; request.InputStream.Read(chunk, 0, Convert.ToInt32(request.ContentLength)); JsonResult returnData = null; string fileSession = "CurrentFile"; if (Session[fileSession] != null) { MVCModels.CloudFile model = (MVCModels.CloudFile)Session[fileSession]; returnData = UploadCurrentChunk(model, chunk, id); if (returnData != null) { return(returnData); } if (id == model.BlockCount) { return(CommitAllChunks(model)); } } else { returnData = Json(new { error = true, isLastBlock = false, message = string.Format(CultureInfo.CurrentCulture, "Failed to Upload file.", "Session Timed out") }); return(returnData); } return(Json(new { error = false, isLastBlock = false, message = string.Empty })); } catch (Exception ex) { Dictionary <string, string> dicContext = new Dictionary <string, string>(); dicContext.Add("Method:", MethodBase.GetCurrentMethod().Name); DataControl.Impl.ExceptionHandler.WriteLog(ex, dicContext); throw ex; } }
private ActionResult CommitAllChunks(MVCModels.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 (Exception e) { model.UploadStatusMessage = "Failed to Upload file. Exception - " + e.Message; errorInOperation = true; Dictionary <string, string> dicContext = new Dictionary <string, string>(); dicContext.Add("Method:", MethodBase.GetCurrentMethod().Name); DataControl.Impl.ExceptionHandler.WriteLog(e, dicContext); } finally { } return(Json(new { error = errorInOperation, isLastBlock = model.IsUploadCompleted, message = model.UploadStatusMessage })); }
private JsonResult UploadCurrentChunk(MVCModels.CloudFile model, byte[] chunk, int id) { try { 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("CurrentFile"); model.IsUploadCompleted = true; model.UploadStatusMessage = "Failed to Upload file. Exception - " + e.Message; return(Json(new { error = true, isLastBlock = false, message = model.UploadStatusMessage })); } } } catch (Exception ex) { Dictionary <string, string> dicContext = new Dictionary <string, string>(); dicContext.Add("Method:", MethodBase.GetCurrentMethod().Name); DataControl.Impl.ExceptionHandler.WriteLog(ex, dicContext); throw ex; } }