Disable Video\Audio Download while using FileStreamResult

Viewed 26

I am using .net core 3.1 in order to stream an audio file.

The code works 100%, but I am trying to disable the download button.

Here is my controller code:

Public IActionResult Play(int id)
            {
                var recording = _recordingRepository.PlayRecording(id);
                var path = recording.FilePath + "\\" + recording.FileName; ;
                if (recording.NewFilePath != null)
                    path = recording.NewFilePath + "\\" + recording.FileName;

                var fs = new FileStream(path, FileMode.Open, FileAccess.Read);


                return new FileStreamResult(fs, "audio/mp3")

                {
                    EnableRangeProcessing = true,
                };
            }

My view has no code. The return of FileStreamResult from the controller automatically creates the video control player. I know this because the browser inspect tool shows that.

Here is what the code looks in the browser when I inspect the player control

<video controls="" autoplay="" name="media"><source src="https://localhost:44330/Player/Play/14837" type="audio/mp3"></video>

I have tried to disable the download feature using javascript, css, nothing works.

Does anyone know how to disable the download feature.

1 Answers

HTML-5 Audio and Video tags have controlslist attribute.

Using JS, you could set controlsList="nodownload" for the Audio/ Video tag to remove the Download option.

Example:

<!DOCTYPE html>
<html>
   <head>
      <title>Demo</title>
      <script>
         function myFunction() {
           document.getElementsByTagName("video")[0].setAttribute("controlsList", "nodownload"); 
           document.getElementsByTagName("audio")[0].setAttribute("controlsList", "nodownload"); 
           document.getElementById("p1").innerText= "Download option disabled for Audio/ Video..."
           
         }
      </script>
   </head>
   <body>
      <h1>Disable Download on Audio/ Video</h1>
      <video width="320" height="240" controls >
         <source src="movie.mp4" type="video/mp4">
         <source src="movie.ogg" type="video/ogg">
         Your browser does not support the video tag.
      </video>
      <br>
      <audio controls >
         <source src="horse.ogg" type="audio/ogg">
         <source src="horse.mp3" type="audio/mpeg">
         Your browser does not support the audio element.
      </audio>
      <br>
      <button onclick="myFunction()">Disable Download on Audio/ Video</button>
      <h3>
         <p id="p1" style="color:red"></p>
      </h3>
   </body>
</html>

Output:

enter image description here

Helpful Reference:

  1. Blocklisting
Related