How to make a video file available for viewing in the browser as PDF?

4

How do you make a file available for download and browser viewing? As an example, files of type pdf and mp4 .

When I want to make the file available for download, I simply use the following routine:

public ActionResult Download(string fileName)
{
    var path = Server.MapPath("~/Arquivos/Biblioteca/") + fileName;
    return File(path, MimeMapping.GetMimeMapping(path), Path.GetFileName(path));
}

When trying to make it available to be viewed in the browser I tried the following:

public ActionResult OpenFile(string fileName)
{
    var path = Server.MapPath("~/Arquivos/Biblioteca/") + fileName;
    return File(path, MimeMapping.GetMimeMapping(path));
}

The difference is that I did not tell the property referring to the file name to download.

On my computer (debug mode mode) the video and PDF are shown in the browser as expected.

But when trying to make it available on the server, both are not opened and the file is then downloaded.

Firstly I was trying to open the file directly, without passing a controller and an action, so time was right, not time, because it gave a file error not found. Probably due to the size of the name.

For this to happen, I needed to register the mime on the server's IIS video server.

But as I said, it is usually a mistake and for good practice, I do not want to access the file directly.

So, how do you make the files available for viewing in your browser, when possible?

    
asked by anonymous 12.05.2015 / 19:17

1 answer

1

You should not serve the video as a file, but rather as a Stream .

To do this, implement a class that prepares the Stream of the video for you:

public class VideoStream
{
   private readonly string _arquivo;

   public VideoStream(string arquivo)
   {
      _filename = @"C:\Seu\Diretorio\De\Video\" + arquivo;
   }

   public async void WriteToStream(Stream outputStream, HttpContent content = null, TransportContext context = null)
   {
      try
      {
          var buffer = new byte[65536];

          using (var video = File.Open(_filename, FileMode.Open, FileAccess.Read))
          {
              var length = (int)video.Length;
              var bytesRead = 1;

              while (length > 0 && bytesRead > 0)
              {
                  bytesRead = video.Read(buffer, 0, Math.Min(length, buffer.Length));
                  await outputStream.WriteAsync(buffer, 0, bytesRead);
                  length -= bytesRead;
              }
          }
      }
      catch (HttpException ex)
      {
         return;
      }
      finally
      {
         outputStream.Close();
      }
   }
}

Here we can have two solutions: one for MVC, another for Web API.

Web API

public class VideosController : ApiController
{
   public HttpResponseMessage Get(string videoNome)
   {
      var video = new VideoStream(videoNome + ".mp4");

      var response = Request.CreateResponse();
      response.Content = new PushStreamContent(video.WriteToStream, new MediaTypeHeaderValue("video/mp4"));

      return response;
   }
}

MVC

Implement before VideoResult :

public class VideoResult : ActionResult 
{ 
    public override void ExecuteResult(ControllerContext context) 
    { 
        var queryString = context.Request.GetQueryNameValuePairs().ToDictionary(x => x.Key, x => x.Value);
        var videoStream = new VideoStream(queryString["videoNome"]);
        // Header
        context.HttpContext.Response.AddHeader("Content-Disposition", "attachment; filename=" + queryString["videoNome"] + ".mp4"); 
        videoStream.WriteToStream(context.HttpContext.Response.OutputStream);
    } 
} 

And Action :

    public ActionResult Index(string videoNome)
    { 
        return new VideoResult(); 
    }
    
12.07.2016 / 20:51