title: Streaming MP4 video through .NET HTML5
body: <p>I am trying to create a test page which will contain a HTML5 VIDEO tag which will allow converted videos to be played.  I am successfully able to convert the videos and store these locally on the server but I'd like to be able to stream all videos through another .aspx page.</p>
  
  <p>Assuming I have a player.aspx page which will contain the HTML code and getvideo.aspx page which will do nothing except provide the video binary, I thought that the following code would work fine in my player.aspx page:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;div style="text-align:center"&gt;   
  &lt;video controls autoplay id="video1" width="920"&gt;  
      &lt;source src="http://www.mywebsite.com/getvideo.aspx?getvideo=1" type="video/mp4"&gt;  
      Your browser does not support HTML5 video.  
  &lt;/video&gt;  
  </code></pre>
  
  <p>The getvideo.aspx page contains the following vb.net code:</p>
  
  <pre><code>Response.clearheaders
  Response.AddHeader("Content-Type", "video/mp4")
  Response.AddHeader("Content-Disposition", "inline;filename=""newvideo.mp4""")
  
  dim Err as string = ""
  Dim iStream As System.IO.Stream
  
  ' Buffer to read 10K bytes in chunk:
  Dim buffer(buffersize) As Byte
  ' Length of the file:
  Dim length As Integer
  
  ' Total bytes to read:
  Dim dataToRead As Long
  
  ' Identify the file to download including its path.
  Dim filepath As String = "./outout/videos/newvideo.mp4"
  
  ' Identify the file name.
  Dim filename As String = System.IO.Path.GetFileName(filepath)
  
  ' Open the file.
  try
      iStream = New System.IO.FileStream(filepath, System.IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
  catch ex as exception
      throw new exception("Could not create FileStream for [" &amp; filepath &amp; "], error follows." &amp; vbcrlf &amp; ex.toString)
  end try
  
  Try
      ' Total bytes to read:
      dataToRead = iStream.Length
  
      ' Read the bytes.
      While dataToRead &gt; 0
          ' Verify that the client is connected.
          If system.web.httpcontext.current.Response.IsClientConnected Then
              ' Read the data in buffer
              length = iStream.Read(buffer, 0, buffersize)
              ' Write the data to the current output stream.
              system.web.httpcontext.current.Response.OutputStream.Write(buffer, 0, length)
             ' Flush the data to the HTML output.
             system.web.httpcontext.current.Response.Flush()
  
             ReDim buffer(buffersize) ' Clear the buffer
             dataToRead = dataToRead - length
         Else
             'prevent infinite loop if user disconnects
             dataToRead = -1
         End If
      End While
  
  Catch ex As Exception
      ' Trap the error, if any.
      err =  "Error accessing " &amp; filepath &amp; " : " &amp; ex.tostring
  Finally
      If IsNothing(iStream) = False Then
          ' Close the file.
          iStream.Close()
      End If
  End Try
  
  if err&lt;&gt;"" then throw new exception( err )
  </code></pre>
  
  <p>All I get on my page output is a HTML video player (chrome's basic player) which seems to time out and the PLAY button goes grey.  The Network tool in the Chrome Developer tools shows that it's downloading 45mb and gets a 200 response code.  This suggests to me that it is working fine.  Although I get a second GET request with the status of "Cancelled"?</p>
  
  <p>If I visit www.mywebsite.com/output/videos/myvideo.mp4 then this plays in the browser fine so I know IIS is configured to correctly stream video.</p>
  
  <p>Also if I change the response content disposition to "attachment" then the browser correctly forces a download of the video when going to my ASPX page, but this also doesn't correctly play on the HTML player.  Is there something 'clever' going on with the HTML5 VIDEO tag which is stopping a .aspx file from serving up video via .net? Or am I missing a response header? </p>
  
  <p>Thanks!</p>