Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create file object of pdf from blob url (URL.createObjectURL())

I have created Object url of PDF file using url: URL.createObjectURL(file[0]).

e.g url = blob:http://0.0.0.0:5002/e468fb70-d597-4b17-bb3a-ec6272f2d7fe.

Now what I want to do is, read pdf from url=blob:http://0.0.0.0:5002/e468fb70-d597-4b17-bb3a-ec6272f2d7fe and create file object like

File(61) 
{
   name: "ICT_zbbihfc_dummy.pdf", 
   lastModified: 1548148972779, 
   lastModifiedDate: Tue Jan 22 2019 17:22:52 GMT+0800 (Singapore Standard Time), 
   webkitRelativePath: "", 
   size: 61,
   type: "application/pdf",
   webkitRelativePath: ""
}

This is in Javascript.

like image 954
Naisarg Parmar Avatar asked Sep 07 '25 09:09

Naisarg Parmar


1 Answers

To convert Blob object url of PDF or Image to File object

var file_object = fetch('blob:http://0.0.0.0:5002/e468fb70-d597-4b17-bb3a-ec6272f2d7fe') 
          .then(r => r.blob())
          .then(blob => {
              var file_name = Math.random().toString(36).substring(6) + '_name.pdf'; //e.g ueq6ge1j_name.pdf
              var file_object = new File([blob], file_name, {type: 'application/pdf'});
              console.log(file_object); //Output
           });
//------- 

To convert Base64 of Image to File object

var file_object = fetch('data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA') 
          .then(r => r.blob())
          .then(blob => {
              var file_name = Math.random().toString(36).substring(6) + '_name.jpeg'; //e.g ueq6ge1j_name.jpeg
              var file_object = new File([blob], file_name, {type: 'application/jpeg'});
              console.log(file_object); //Output
           });
//------- 
like image 62
Naisarg Parmar Avatar answered Sep 08 '25 22:09

Naisarg Parmar