private async Task <bool> SaveToAzureBlob(BlobDetails blobDetails) { MemoryStream ms = null; IFormFile file = null; try { if (CloudStorageAccount.TryParse(_azureBlobConfig.Value.ConnectionString, out CloudStorageAccount storageAccount)) { CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = BlobUtilities.GetBlobContainer(blobClient, blobDetails.Type, _azureBlobConfig); CloudBlockBlob blockBlob = container.GetBlockBlobReference(BlobUtilities.GenerateUniqueAadhaarImageName(blobDetails.CustomerID, blobDetails.Type)); using (ms = new MemoryStream()) { file = blobDetails.BlobFile; file.CopyTo(ms); var fileBytes = ms.ToArray(); await blockBlob.UploadFromByteArrayAsync(fileBytes, 0, fileBytes.Length); } return(true); } else { return(false); } } catch { return(false); } //finally //{ // await ms.DisposeAsync(); //} }
public async Task <bool> InsertFile(string customerID, BlobType type = BlobType.Aadhaar) { IFormFile file = null; byte[] fileByteContent = null; if (Request.Form.Files.Count > 0) { file = Request.Form.Files[0]; } else { var input = Request.Form.AsEnumerable(); foreach (var keyvalue in input) { if (keyvalue.Key.Equals("customerID")) { customerID = keyvalue.Value; } else if (keyvalue.Key.Equals("type")) { var type1 = keyvalue.Value.ToString(); type = BlobUtilities.GetBlobType(type1); } else { var byteData = keyvalue.Value; var fileContent = byteData.ToString(); fileByteContent = Encoding.UTF8.GetBytes(fileContent); } } } try { if (CloudStorageAccount.TryParse(_azureBlobConfig.Value.ConnectionString, out CloudStorageAccount storageAccount)) { CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = BlobUtilities.GetBlobContainer(blobClient, type, _azureBlobConfig); CloudBlockBlob blockBlob = container.GetBlockBlobReference(BlobUtilities.GenerateUniqueAadhaarImageName(customerID, type)); if (file != null) { await blockBlob.UploadFromStreamAsync(file.OpenReadStream()); } else { await blockBlob.UploadFromByteArrayAsync(fileByteContent, 0, fileByteContent.Length); } return(true); } else { return(false); } } catch { return(false); } }