I am trying to upload a PDF file from postman and trigger the azure function to upload the PDF file into azure blob storage. But when i try to open the PDF file it is always empty.
I tried to convert the file into memory stream and upload it into azure blob. The file gets uploaded but when i try to open the file it will be blank.
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
{
log.Info(req.Content.ToString());
string Message = "";
log.Info("Test storage conn string" + req.Content.Headers.ContentDisposition.ToString());
string contentType = req.Content.Headers?.ContentType?.MediaType;
log.Info("contentType : " + req.Content.IsMimeMultipartContent());
string name = Guid.NewGuid().ToString("n");
log.Info("Name" + name);
string body;
body = await req.Content.ReadAsStringAsync();
log.Info("body" + body.Substring(body.IndexOf("filename=\""),body.IndexOf("pdf")- body.IndexOf("filename=\"")));
//Upload a file to Azure blob
string storageConnectionString = "xxxx";
//DirectoryInfo directoryInfo = new DirectoryInfo("D:\\Upload_Files");
// var files = directoryInfo.EnumerateFiles();
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("docstorage");
//foreach (FileInfo inputFile in files)
//{
CloudBlockBlob blockBlob = container.GetBlockBlobReference("Test\\" + name+".pdf");//write name here
//blockBlob.Properties.ContentType = "application/pdf";
//blockBlob.UploadFromFile(inputFile.FullName);
using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(body)))
{
log.Info("streaming : ");
await blockBlob.UploadFromStreamAsync(stream);
}
//}
return Message == null
? req.CreateResponse(HttpStatusCode.BadRequest, "Error")
: req.CreateResponse(HttpStatusCode.OK, "Doc Uploaded Successfully");
}
I want to open the PDF file as it is from the blob. I see that i am able to upload text file and when i download i can see the content but when i upload pdf file i dont see the content