3

I am incidently using react but the fact remains: when I put up a simple page with an input to upload a local file, if I console.log the actual file once it has been selected, here is what I get from the console:

File {name: "myfile.mp4", lastModified: 1474084788000, lastModifiedDate: Fri Sep 16 2016 23:59:48 GMT-0400 (EDT), webkitRelativePath: "", size: 27828905…}
lastModified: 1474084788000
lastModifiedDate: Fri Sep 16 2016 23:59:48 GMT-0400 (EDT)
name: "myfile.mp4"
size: 27828905
type: "video/mp4"
webkitRelativePath: ""
__proto__: File

And so the file loads in the video tags and I can watch it. (The code is below...)

Then, if I want to load the same file but from a hardcoded full path instead, like so: "file:///path/to/myfile.mp4", an error pops up This video file format is not supported. and what I get back from the console is the exact same path that I had previously hardcoded.

My question is how should one load a local file by using a hardcoded path instead of selecting a file from an input element?
OR
How to create an objectURL directly from a local path?

I've already tried to Blob the file before passing it on to the URL.createObjectURL function but, unless I did it something wrong, it didn't work out.

Render function code:

  render() {

    return (
      <div>
        <input type="file" ref="input" onChange={this.upload} />

        <video ref="video" type="video/mp4" controls loop autoPlay width="720"></video>  
        <div ref="message"></div>
      </div>
    );
  }

Functions:

  processs = (file) => {
      let fileURL = URL.createObjectURL(file);
      this.refs.video.src = fileURL;
  }

  playFile = (event) => {
    let file = event.target.files[0];
    console.log(file);

    //check if video can be played
    if(this.refs.video.canPlayType(file.type) != ""){
      this.processs(file);
    } else {
      this.refs.message.innerHTML = "This video file format is not supported."
    }

  };
Jeanmichel Cote
  • 531
  • 1
  • 5
  • 19
  • What do you mean by _"load the same file but from a hardcoded full path instead"_? Setting the ` – guest271314 Sep 17 '16 at 16:38
  • In react, instead of passing the file as `const file = event.target.files[0]` from the input element, I just created a `this.state({myfile: "file:///fullpath/to/myfile.mp4"})` in the constructor and refer to it as `const file = this.state.myfile` – Jeanmichel Cote Sep 17 '16 at 16:41
  • You can use `XMLHttpRequest()` to request local file as `Blob` – guest271314 Sep 17 '16 at 16:46

2 Answers2

-1

How to create an objectURL directly from a local path?

You can use XMLHttpRequest with responseType set to "blob". File inherits from Blob, you can pass returned Blob to your existing function which expects File object. See also Loading images from file with Javascript

var request = new XMLHttpRequest();
request.responseType = "blob";
request.open("GET", "file:///path/to/myfile.mp4");
request.onload = () => {
  // do stuff with `request.response` : `Blob`
}
request.send();
Community
  • 1
  • 1
guest271314
  • 1
  • 15
  • 104
  • 177
-1

Here is a working demo

Load Image or Video without Upload

Hope it helps

Manoj Sethi
  • 1,898
  • 8
  • 26
  • 56