title: How do I stream a video file using ASP.NET MVC?
body: <p>I am developing a web site where people can access audio and video files. I have the code for downloading the files, which consists of two action methods as follows...</p>
  
  <pre><code>public ActionResult GetAudioFile(int id) {
    return GetFile(id, true);
  }
  
  public ActionResult GetVideoFile(int id) {
    return GetFile(id, false);
  }
  
  private ActionResult GetFile(int id, bool isAudio) {
    // Code to get info about the file, etc omitted for clarity
    string downloadFileName = // full path to audio/video file
    byte[] bytes = GetFileBytes(fileName); // also omitted
    return File(bytes, (isAudio ? "audio/mpeg" : "video/mp4"), downloadFileName + (isAudio ? ".mp3" : ".mp4"));
  }
  </code></pre>
  
  <p>These both work fine, and I can download either type of file.</p>
  
  <p>I now want to add two Razor views, one for listening to the audio file, and one for viewing the video. I did the following on the audio view, and it works fine...</p>
  
  <pre><code>&lt;audio src='@Url.Action("GetAudioFile", "Search", new {ID = @Model.ID})'
                 controls preload="auto"&gt;&lt;/audio&gt;
  </code></pre>
  
  <p>I then tried to do the same for the video view...</p>
  
  <pre><code>&lt;video src='@Url.Action("GetVideoFile", "Search", new {ID = @Model.ID})' 
                 controls preload="auto" width="640" height="368"&gt;&lt;/video&gt;
  </code></pre>
  
  <p>However, this gives a System.OutOfMemoryException when I try to run it. The videos average around 400-500Mb each.</p>
  
  <p>I then tried using the Response.TransmitFile method as follows...</p>
  
  <pre><code>  Response.ContentType = "video/mp4";
    Response.AddHeader("content-disposition", "attachment; filename=myvideo.mp4"));
    Response.TransmitFile(fileName);
    Response.End();
    return null;
  </code></pre>
  
  <p>...but that doesn't work. In Chrome, I can see the message "Resource interpreted as Document but transferred with MIME type video/mp4" in the console, and in FireFox, I get the same message in the video control.</p>
  
  <p>Anyone any idea how I can fix this? Ideally, I would like to stream the file, so that it starts playing as soon as the first bytes reach the user, rather than having them wait until the file is completely downloaded.</p>
  
  <p>I've tried a couple of Javascript video players, but had any success with those either.</p>
  
  <p><strong>Update</strong> I'm wondering if it's not the controller action that's the problem at all, as I tried pointing my web browser directly at the video file, and I got what looked like an audio player, but without a video pane, and nothing happens when I click the play button. Not sure if that helps.</p>