Show document . PDF in browser via Azure Function

Asked

Viewed 15 times

0

Azure Function then takes the document ". pdf" and download it, what I want to know is a way to grab that document, save it in the browser and display it on a web page

I wonder if I should return another format, or even turn Stream into Base64.

namespace downloadFileBlob
{
  public static class BlobOperations
  {
    [FunctionName("DownloadBlob")]public static async Task<HttpResponseMessage> DownloadBlob([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "Download/{blobContainer}/{serialNumber}")] 
    HttpRequest req, string serialNumber, string blobContainer
  )
  {
    string azure_connString = "KeyString";

    string table_azure = "requisicoes";
     
    Microsoft.Azure.Cosmos.Table.CloudStorageAccountstorageAccountAzure = Microsoft.Azure.Cosmos.Table.CloudStorageAccount.Parse(azure_connString);
    CloudTable cloudTable = storageAccountAzure.CreateCloudTableClient().GetTableReference(table_azure);
    var requisicao = cloudTable.CreateQuery<RequisicaoEntity>().Where(y => y.PartitionKey == serialNumber && y.RowKey == blobContainer).FirstOrDefault();
    Microsoft.Azure.Storage.CloudStorageAccount storageAccount = Microsoft.Azure.Storage.CloudStorageAccount.Parse(azure_connString);

    CloudBlobContainer blobClient = storageAccount.CreateCloudBlobClient().GetContainerReference(requisicao.BlobContainer);
    CloudBlobDirectory blobDirectory = blobClient.GetDirectoryReference(requisicao.Pasta);
    CloudBlockBlob block = blobDirectory.GetBlockBlobReference(requisicao.NomeArquivo);
    HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.OK);
    Stream blobStream = await block.OpenReadAsync();

    message.Content = new StreamContent(blobStream);
    message.Content.Headers.ContentLength = block.Properties.Length;
    message.StatusCode = HttpStatusCode.OK;
    message.Content.Headers.ContentType = new MediaTypeHeaderValue(block.Properties.ContentType);
    message.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
        FileName = $"CopyOf_{block.Name}",
        Size = block.Properties.Length
    };

    return message;
  }

 public class RequisicaoEntity:TableEntity
  {
    public string BlobContainer {get;set;}
    public string Pasta {get; set;}
    public string NomeArquivo {get;set;}
    public string NumeroSerie {get;set;}
  }
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.