1. 程式人生 > >關於Not allowed to load local resource的問題

關於Not allowed to load local resource的問題

在瀏覽器裡面,本來想將下面的黃色程式碼程式碼改成file,以為這樣可以選擇播放的檔案,但是實際上是不行的,因為瀏覽器會保護,不允許載入如
C:\fakepath\testVideo.mp4這樣的絕對路徑,必須使用相對路徑,將視訊和網頁檔案放在一起,然後在網頁輸入檔名就可以正常播放
像下圖
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>視訊播放器</title>
    <script type="text/javascript"
> function playOrPauseVideo() { var videoUrl = document.getElementById("videoUrl").value; console.log(videoUrl); var video = document.getElementById("video"); video.addEventListener("timeupdate",function() {
var timeDisplay = document.getElementById("time"); timeDisplay.innerHTML = Math.floor(video.currentTime)+"/"+Math.floor(video.duration)+"(秒)"; },false); if(video.paused) { if (videoUrl != video.src) { video.src = videoUrl;
video.load(); }else { video.play(); } document.getElementById("playButton").value ="暫停"; }else { video.pause(); document.getElementById("playButton").value ="播放"; } } </script> </head> <body> <video id="video" width="400" height="300" autoplay loop="loop"> </video> <br /> 視訊地址:<input type="text" id="videoUrl"/> <input id="playButton" type="button" onclick="playOrPauseVideo()" value="播放"/> <span id="time"></span> </body> </html>