Create media from blob I am receiving a payload with blob files.

I would like to store them as files and create a media entity.

Here's the code I have to create the Media object

 if (!empty($payload['blobFile'])) {
  $name = str_replace(' ', '_', $payload['fileName']);
  $file = file_save_data($payload['blobFile'], 'public://' . $name, FILE_EXISTS_REPLACE);
  $content['field_media_file'] = ['target_id' =&gt; $file-&gt;id()];
}
$this-&gt;entityTypeManager-&gt;getStorage(....


It successfully creates the media content and attach a file to it.

Unfortunately the file is still blob I don't see a pdf or similar.
A file that comes in from a browser's Web API as a Blob is probably base64-encoded. PHP will not decode files in the POST or GET parameters. It is up to the PHP application to decode the file before saving.

if (!empty($payload['blobFile'])) {
  $name = str_replace(' ', '_', $payload['fileName']);
  $file = file_save_data(base64_decode($payload['blobFile']), 'public://' . $name, FILE_EXISTS_REPLACE);
  $content['field_media_file'] = ['target_id' =&gt; $file-&gt;id()];
}


It might also be useful to check if the binary is actually a PDF before saving, but that could be done in different ways based on opinion (loading via a PDF library, checking file headers, virus/malware scan).