The Objective: Utilize a POST request to send a PDF file, store its blob content in Azure Storage, and retrieve the content for display in a web browser
Current Progress: The code successfully triggers the controller's POST method with PDF file content, which is then returned as a response that Angular displays in the browser.
Angular/HTML Code:
//html
<object ng-show="content" data="{{content}}" type="application/pdf" style="width: 100%; height: 400px;"></object>
//angular controller
...
.success(function (data) {
console.log(data);
var file = new Blob([(data)], { type: 'application/pdf' });
var fileURL = URL.createObjectURL(file);
$scope.content = $sce.trustAsResourceUrl(fileURL);
}
WebAPI Controller:
// POST api/<something>/Upload
[Authorize]
[HttpPost]
[Route("Upload")]
public async Task<HttpResponseMessage> Post()
{
try
{
HttpRequestMessage request = this.Request;
if (!request.Content.IsMimeMultipartContent())
{
return new HttpResponseMessage(HttpStatusCode.UnsupportedMediaType);
//return StatusCode(HttpStatusCode.UnsupportedMediaType);
}
var customMultipartFormDataProvider = new CustomMultipartFormDataProvider();
var provider = await request.Content.ReadAsMultipartAsync<CustomMultipartFormDataProvider>(customMultipartFormDataProvider);
//contents[1] and //contents[2] were StreamContent of the FormData
var fileContent = provider.Contents[2];
var formData = provider.FormData;
//can succesfully write to a SQL database here without fail
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = fileContent;
return response;
}
public class CustomMultipartFormDataProvider : MultipartFormDataRemoteStreamProvider
{
public override RemoteStreamInfo GetRemoteStream(HttpContent parent, HttpContentHeaders headers)
{
return new RemoteStreamInfo(
remoteStream: new MemoryStream(),
location: string.Empty,
fileName: string.Empty);
}
}
The Issue at Hand: Unfortunately, when attempting to upload this content to Azure Storage, it encounters some challenges:
string blobStorageConnectionString = ConfigurationManager.ConnectionStrings["AzureStorageAccount"].ConnectionString;
CloudStorageAccount blobStorageAccount = CloudStorageAccount.Parse(blobStorageConnectionString);
CloudBlobClient blobClient = blobStorageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(<containerName>);
container.CreateIfNotExists();
CloudBlockBlob block = container.GetBlockBlobReference(<keyname>);
block.UploadFromStream(await fileContent.ReadAsStreamAsync());
While the upload process to storage seems successful and the control flow reaches the return statement in the WebAPI controller, it does not seem to complete.
The "console.log(data)" within my controller's success function never gets triggered. It appears that the return statement may be failing to execute properly despite seeming to do so.