I have an ASP.NET Web API project that I'm building that requires uploading word documents using Azure blob storage. I have a method that creates a user profile but unfortunately, I'm encountering issues related to azure blob storage and uploading a document. When debugging, the IFormFile document is null, and the connection to azure is null as well. I can't figure out where the disconnect is. I'd appreciate if someone knows the solution. Below is the code that I've written.
Model
Person Model
public int Id {get; set;}
public string FirstName {get; set;}
public string LastName {get;set;}
public string URL {get;set;}
public string DocName {get;set;}
public string IFormFile[] DocUpload {get;set;}
Repository
public class DocRepo : IDocRepo
{
private CloudStorageAccount storageAccount = null;
private CloudBlobContainer cloudBlobContainer = null;
public IConfiguration _config { get; }
private readonly ILogger _logger;
public DocRepo(ILogger logger)
{
_logger = logger;
}
public void Configure()
{
var AzConnectionString = _config["AzConnectionString"];
if (CloudStorageAccount.TryParse(AzConnectionString, out storageAccount))
{
try
{
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
cloudBlobContainer = cloudBlobClient.GetContainerReference("folder");
cloudBlobContainer.CreateAsync().GetAwaiter().GetResult();
BlobContainerPermissions permissions = new BlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
};
cloudBlobContainer.SetPermissionsAsync(permissions).GetAwaiter().GetResult();
}
catch (StorageException ex)
{
_logger.LogError("Error returned from the service: {0}", ex);
}
_logger.LogInformation("Success");
}
else
{
_logger.LogInformation("No Connection");
}
}
public async Task Upload(IFormFile[] docuFile)
{
Person person = new Person();
person.DocUpload = docuFile;
List<string> url = new List<string>();
List<string> docName = new List<string>();
try
{
if (docuFile != null)
{
foreach (var file in docuFile)
{
if (file.ContentType == "application/msword")
{
if (file.Length < 5 * 1400 * 1400)
{
var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
var filename = parsedContentDisposition.FileName.Trim('"');
docName.Add(filename);
filename = Guid.NewGuid().ToString() + "-" + filename;
CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(filename);
cloudBlockBlob.Properties.ContentType = file.ContentType;
await cloudBlockBlob.UploadFromStreamAsync(file.OpenReadStream());
url.Add(cloudBlockBlob.Uri.AbsoluteUri);
}
else
{
_logger.LogInformation("5 mb max size allowed");
}
}
else
{
_logger.LogInformation("Only .doc format accepted");
}
}
person.URL = (urls.Count > 0) ? string.Join(" || ", urls) : null;
person.DocName = string.Join(" || ", name);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error uploading");
}
}
Interface
public interface IDocRepo
{
void Configure();
Task Upload(IFormFile[] doc);
}
PersonController
[HttpPost]
public async Task<IActionResult> Create([FromBody] Person person)
{
if (ModelState.IsValid)
{
try
{
_docRepo.CreateConfigure();
await _docRepo.Upload(person.DocUpload);
_context.Person.Add(person);
await _context.SaveChanges();
}
catch (Exception ex)
{
throw ex;
}
}
return CreatedAtAction("PersonCreated", new { id = person.Id }, person);
}