I’m having trouble trying to save a PDF to Azure blob storage.
PDF is coming from SPA to Azure Function App, this is what I have currently
POST Request from SPA
const uploadFunction = async () => {
if (!selectedDocuments || !targetLanguage) return;
const formData = new FormData();
formData.append("targetLanguage", targetLanguage);
[...selectedDocuments].map((document, index) => {
formData.append(`document${index + 1}`, document);
});
const translate = await fetch("http://localhost/api/TranslateFunction", {
method: "POST",
body: formData,
headers: {
"Content-Type": "undefined",
},
});
console.log(translate);
};
Then in my Azure Function App I have this
[Function("TranslateFunction")]
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestData req, FunctionContext executionContent)
{
string connection = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
string sourceContainer = Environment.GetEnvironmentVariable("SourceContainer");
string targetContainer = Environment.GetEnvironmentVariable("TargetContainer");
var sourceContainerClient = new BlobContainerClient(connection, sourceContainer);
var targetContainerClient = new BlobContainerClient(connection, targetContainer);
string body = await new StreamReader(req.Body).ReadToEndAsync();
var response = req.CreateResponse(HttpStatusCode.OK);
return response;
}
I believe, though I’m not sure, that PDF is received in the body of HttpRequestData.
But I dont know how to now send this string as PDF to blob storage. Do I need to use a NuGet package to convert string to PDF and then upload that `?
There are tutorials on how to save PDF to Blob Storage, but they start by getting the PDF document from filesystem, but in my case its coming through http request.
EDIT:
Inspecting the “body” after StreamReader, it shows pdf is received
------WebKitFormBoundaryxQpq3BGLOOwB4z2I
Content-Disposition: form-data; name="document1"; filename="one.pdf"
Content-Type: application/pdf
%PDF-1.4
%����
1 0 obj
<</Type /Catalog
/Pages 2 0 R
/PageLayout /TwoColumnRight
/Outlines 7 0 R>>
endobj
9 0 obj
<</Title (Cutaneous receptors)
/Parent 8 0 R
/Dest [3 0 R /XYZ 0 420 0]
/Next 10 0 R>>
endobj
10 0 obj
<</Title (Nociceptors)
/Parent 8 0 R
/Dest [4 0 R /XYZ 0 124 0]
/Prev 9 0 R
/Next 11 0 R>>
endobj
11 0 obj
<</Title (Muscle Spindles)
/Parent 8 0 R
/Dest [5 0 R /XYZ 0 201 0]
/Prev 10 0 R
/Next 12 0 R>>
endobj
12 0 obj
<</Title (Joint receptors)
/Parent 8 0 R
/Dest [6 0 R /XYZ 0 174 0]
/Prev 11 0 R>>
endobj
8 0 obj
<</Title (Anatomy of the Somatosensory System)
/Parent 7 0 R
/Dest [3 0 R /XYZ 0 624 0]
/First 9 0 R
/Last 12 0 R
/Count 4>>
endobj
7 0 obj
<</Type /Outlines
/First 8 0 R
/Last 8 0 R>>
endobj
2 0 obj
<</Type /Pages
/Kids [3 0 R 4 0 R 5 0 R 6 0 R]
/Count 4>>
endobj
20 0 obj
<</Length 21 0 R
/Filter /FlateDecode>>
stream
x���َe�r�^_����f�
you need to save PDF file to binary[] and upload it, I do it in that way:
output_file_bytes = SrtingToByte(current_pdf)
BlobServiceClient blobServiceClient = new BlobServiceClient(blobConnectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(blobContainerName);
BlobClient blobClient = containerClient.GetBlobClient(current_file_name);
using (MemoryStream ms = new MemoryStream(output_file_bytes))
{
blobClient.Upload(ms, overwrite: true);
}
blobUri = blobClient.Uri.ToString();
2