I am sending a PDF file to my WebAPI Controller via a POST request using Angular as such:
$scope.upload = Upload.upload({
url: "../api/Locker/Upload", // webapi url
method: "POST",
file: controllerFile,
})
which in my POST method on my controller I get the StreamContent of that file as follows:
public async Task<HttpResponseMessage> Post()
{
HttpRequestMessage request = this.Request;
if (!request.Content.IsMimeMultipartContent())
{
request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
}
var result = await Request.Content.ReadAsMultipartAsync<CustomMultipartFormDataProvider>(new CustomMultipartFormDataProvider());
string fileName = result.FileData[0].Headers.ContentDisposition.FileName;
var fileData = result.Contents[0];
}
It is saying that results.Contents[0] is of type HttpContent but in the Immediate window when I type fileData it says it is of StreamContent type.
I am trying to upload to Azure Blob Storage this fileData so that I can then retrieve it using a GET request later on, but am having trouble doing so.
//in post method
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(blobConnectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(blobContainer);
await container.CreateIfNotExistsAsync();
string blobId = Guid.NewGuid().ToString();
UploadToBlob(container, blobId, fileData);
and method where I am stuck:
private async void UploadToBlob(CloudBlobContainer container, string blobId, HttpContent fileData)
{
CloudBlockBlob block = container.GetBlockBlobReference(blobId);
block.UploadFromStream(fileData);
}
error on block.UploadFromStream because fileData is not a Stream of course.
What can I do if I am expecting a HTTP Response with content being of type: arraybuffer so that I can expose the file in my web application such as:
//angular get request
.success(function (data, status, headers, config) {
// file is uploaded successfully
console.log(data);
var fileBack = new Blob([(data)], { type: 'application/pdf' });
var fileURL = URL.createObjectURL(fileBack);
}