Prevent HTML5 video from being downloaded (right-click saved)?

前端 未结 20 2389
醉酒成梦
醉酒成梦 2020-11-22 15:33

How can I disable \"Save Video As...\" from a browser\'s right-click menu to prevent clients from downloading a video?

Are there more complete solutions that prevent

20条回答
  •  盖世英雄少女心
    2020-11-22 16:19

    As a client-side developer I recommend to use blob URL, blob URL is a client-side URL which refers to a binary object

    
    

    in HTML leave your video src blank, and in JS fetch the video file using AJAX, make sure the response type is blob

    window.onload = function() {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', 'mov_bbb.mp4', true);
        xhr.responseType = 'blob'; //important
        xhr.onload = function(e) {
            if (this.status == 200) {
                console.log("loaded");
                var blob = this.response;
                var video = document.getElementById('id');
                video.oncanplaythrough = function() {
                    console.log("Can play through video without stopping");
                    URL.revokeObjectURL(this.src);
                };
                video.src = URL.createObjectURL(blob);
                video.load();
            }
        };
        xhr.send();
    }
    

    Note: This method is not recommended for large file

    EDIT

    • Use cross-origin blocking for avoiding direct downloading

    • if the video is delivered by an API Use different method (PUT/POST) instead of 'GET'

提交回复
热议问题