I’ve got a video stored locally on my computer in a folder called assets
. I’m importing the file currently like this into my project:
import sample1 from './assets/sample1.mp4';
I want to use the file as a File()
or Blob()
object, however I do not know how I could convert the string path Webpack is giving me to one of those objects.
I am using React with create-react-app
, so changing the Webpack configuration would not be ideal.
I thought the FileReader()
object would solve my problem, however it only accepts Blob()
objects.
2
Answers
I'm not using an ideal solution, however this works and the Webpack configuration does not need to be changed.
@somethingthere's solution would work, if the file would be directly imported as a base64 encoded string, however the Webpack configuration from
create-react-app
handles this differently and only the file path is contained in the imported variable. But I hadn't come to this solution without her/him.cat base64.txt | xclip -selection clipboard
.data:video/mp4;base64,YOURSTRING
.Done!
You can convert it to a blob using the
Blob
constructor as such:The first argument is an array of chunks (of which you have 1, so one entry), the second is some metadata. By setting the type there it should read the data correctly. You can find out more at the docs about
Blob
: https://developer.mozilla.org/en-US/docs/Web/API/Blob