What if FileReader readAsDataURL failed to preview the video file all the time?

using FileReader readAsDataURL to read locally uploaded video files is often not previewed as shown in figure


The

code is as follows
var reader = new FileReader ();

            reader.onload = (evt) => {
                if (videoType.indexOf(fileType.toLocaleLowerCase()) !== -1) {
                    $(".waitUpload .nullVideo").remove();
                    $(".waitUpload").html(
                        "<div data-filename="" + fileName +
                        "">\
                    <div>\
                        <div class="videoCtn"><video src="" +
                        evt.target.result +
                        "" controls></video></div>\
                    </div>\
                    <div class="videoInfo">\
                        <span class="videoName" title="" +
                        fileName + "">" +
                        fileName + "</span>\
                        <span class="videoSize">" +
                        size +
                        "M</span>\
                    </div>\
                    <div class="btnCtn">\
                        <span class="btn" data-toggle="modal" data-target=".modal"></span>\
                    </div>\
                </div>"
                    );
                    file = _this.files[0];
                    localAddress = evt.target.result;
                } else {
                    alert("");
                }
                $(this).val("");
            };
            reader.readAsDataURL(this.files[0]);

Apr.09,2021

FileReader.readAsDataURL after reading the file is the base64-encoded string, which cannot be directly used as src . If you want to use it directly, you should also concatenate the response MIME Type prefix, such as data:audio/ogg; , which is a prefix in the format .ogg , depending on the extension type of the file you upload.

in fact, there is no need to use FileReader to accomplish this requirement. It is OK to read some small files, but it is not very good to read large files. You might as well directly use URL.createObjectURL () to create a DOMString , and then use this DOMString directly, but don't forget to release it through the URL.revokeObjectURL () method after you finish using it.

I have previously made a very simple previewer component, which is done through the latter. Basic simple formats (audio, video, pictures, etc.) can be previewed, but based on Angular, link .


as a matter of fact, it has already been said upstairs. Let me tell you what I know again.

base64 transcoding actually takes a lot of time, and it still gets stuck. When you get bigger, you can't solve it. You can solve it with a video of three to five seconds.

demo address

Menu