I have created Object url of PDF file using url: URL.createObjectURL(file[0]).
e.g url = blob::5002/e468fb70-d597-4b17-bb3a-ec6272f2d7fe.
Now what I want to do is, read pdf from url=blob::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.
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.
Share Improve this question asked Jan 22, 2019 at 9:32 Naisarg ParmarNaisarg Parmar 80110 silver badges27 bronze badges 2-
This sounds like an X/Y problem. Why do you want to create a
File
object from aBlob
? (Separately: I very much doubt it's possible, but you probably don't need to anyway...) – T.J. Crowder Commented Jan 22, 2019 at 9:37 - Thank you @T.J.Crowder for replying. I am selecting files from input tag. after then I am creating blob url of it. and now on submit button I want to read file from blob url as File Object..then send File Object to server. – Naisarg Parmar Commented Jan 22, 2019 at 9:42
2 Answers
Reset to default 5To 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
});
//-------
In a ment you clarified:
I am selecting files from input tag. after then I am creating blob url of it. and now on submit button I want to read file from blob url as File Object..then send File Object to server.
You don't need a File
object to do that. You can simply send the Blob
, for instance using FormData
's append
method, which accepts Blob
s.
However: If you have the input
, and you've got the File
from the input
's files
array, you may as well just use it directly rather than reading it and creating a Blob
.
It's probably worth pointing out that input type="file"
elements have a multiple
attribute that can be used to specify that it can be used for multiple files (which is why the files
property is a collection).