public async Task <string> UploadBlobAsync(HttpPostedFileBase file, string containerName) { //check file size and extension if (FileUploadUtility.InvalidFileSize(file)) { throw new FileLoadException("The file cannot be bigger than 7MB."); } if (FileUploadUtility.InvalidFileExtension(file)) { throw new FileLoadException("The file type is not authorized for upload."); } CloudBlobContainer container = GetCloudBlobContainer(containerName); await container.CreateIfNotExistsAsync(BlobContainerPublicAccessType.Blob, null, null); //build filename with timestamp to reduce risk of duplicates string blobName = FileUploadUtility.GetFilenameWithTimestamp(file.FileName); CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName); try { await blockBlob.UploadFromStreamAsync(file.InputStream); return(blockBlob.Uri.ToString()); } catch (Exception e) { throw new Exception(e.Message); } }
public static String GetFilenameWithTimestamp(string filename) { return(Path.GetFileNameWithoutExtension(filename) + "_" + FileUploadUtility.GetTimestamp(DateTime.Now) + Path.GetExtension(filename)); }