How to give a "refresh" after updating the new video?

Asked

Viewed 147 times

3

In my view, I have an old video, when I "upload" new video, want to automatically reload a new video after "upload".

Follows the code View:

<div id="embed_video" class="embed-responsive embed-responsive-16by9">
    <video class="embed-responsive-item" controls>
        <source src="/Files/GetVideo?id=2" type="video/mp4">
        Seu navegador não suporta vídeo em HTML5.
    </video>
</div>

Follows code Javascript:

 $('#embed_video video').get(0).load();

Javascript code only works the first time, the second time the video does not update, is as old.

Follows code Controller:

public ActionResult GetVideo(int id)
{
    using (var ctx = new Entities())
    {
        var result = ctx
            .Video
            .FirstOrDefault();    
        byte[] video_byte = result.Video;    
        return new RangeFileContentResult(video_byte, "video/mp4", "NomeDoArquivo.mp4", date);
    }
}

Some solution ?

From now on, thank you.

  • 1

    Dear Matheus, I had wrong variables in the script, I also added another check, now I think the code is 100%, if I can update to the name is better.

  • @Guilhermenascimento Yes I saw that, I didn’t warn you.

2 answers

1


This is because it is cached, because probably the name is the same, one way to solve is to use a random querystring (which is not repeated, as something based on timestamp).

However first it is necessary to catch the currentSrc (https://html.spec.whatwg.org/multipage/media.html#dom-media-currentsrc), something like:

var videourl = $('#embed_video video').prop("currentSrc");

Then clear the querystring:

videourl = videourl.replace(/\?(.*?&)?_=\d+$/, "?$1");

And apply the new:

videurl = videorl + "_=" + Date.now();

You can do a function like this:

function updateVideo(query, play) {
    var el = $(query).get(0); //usei o .get para ficar mais "limpo" o código, mas o resultado é indiferente no final

    if (!el) alert('Não encontrado'); return;

    var videourl = el.currentSrc;

    videourl = videourl.replace(/\?(.*?&)?_=\d+$/, "?$1");
    videourl = videourl + (videourl.indexOf("?") !== - 1 ? "&" : "?");
    videourl = videourl + "_=" + Date.now();

    el.pause();
    el.src = videourl;

    if (play) el.play(); //o play já executa o load
}

The use would be something like:

updateVideo('#embed_video video', true);

Or without autoplay:

updateVideo('#embed_video video', false);

0

I think I answered that same question or a very similar one yesterday... but goes below:

document.getElementById("VideoReload").load();
  • I’ve tried and nothing happens, it remains the old video.

  • But if you directly refer to your action, it returns the correct content?

  • It works only the first time when calling action, in the second, third etc... never calls action.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.