我想实现以下目标。
<video src="file:///Users/username/folder/video.webm">
</video>
这个功能的意图是让用户能够从他/她的硬盘中选择一个文件。
并且不进行上传的原因当然是传输成本和存储配额。没有保存文件的理由。
这可行吗?
我想实现以下目标。
<video src="file:///Users/username/folder/video.webm">
</video>
这个功能的意图是让用户能够从他/她的硬盘中选择一个文件。
并且不进行上传的原因当然是传输成本和存储配额。没有保存文件的理由。
这可行吗?
<input type="file" accept="video/*"/>
<video controls autoplay></video>
input
元素选择文件时:
input.files
FileList获取第一个File对象video.src
属性的值放松并享受观看 :)
http://jsfiddle.net/dsbonev/cCCZ2/embedded/result,js,html,css/
(function localFileVideoPlayer() {
'use strict'
var URL = window.URL || window.webkitURL
var displayMessage = function(message, isError) {
var element = document.querySelector('#message')
element.innerHTML = message
element.className = isError ? 'error' : 'info'
}
var playSelectedFile = function(event) {
var file = this.files[0]
var type = file.type
var videoNode = document.querySelector('video')
var canPlay = videoNode.canPlayType(type)
if (canPlay === '') canPlay = 'no'
var message = 'Can play type "' + type + '": ' + canPlay
var isError = canPlay === 'no'
displayMessage(message, isError)
if (isError) {
return
}
var fileURL = URL.createObjectURL(file)
videoNode.src = fileURL
}
var inputNode = document.querySelector('input')
inputNode.addEventListener('change', playSelectedFile, false)
})()
video,
input {
display: block;
}
input {
width: 100%;
}
.info {
background-color: aqua;
}
.error {
background-color: red;
color: white;
}
<h1>HTML5 local video file player example</h1>
<div id="message"></div>
<input type="file" accept="video/*" />
<video controls autoplay></video>
只有在以“file”协议从本地用户硬盘加载HTML文件时,才能实现这一点。
如果HTML页面由服务器通过HTTP提供服务,您无法通过在src
属性中指定具有file://
协议的本地文件来访问任何本地文件,因为这将意味着您可以访问用户计算机上的任何文件,而不需要用户知道,这将是一个巨大的安全风险。
正如Dimitar Bonev所说,如果用户自己使用文件选择器选择文件,则可以访问文件。没有这个步骤,出于很好的原因,所有浏览器都禁止访问。因此,虽然他的答案对许多人可能很有用,但它放宽了原始问题中代码的要求。
<video src="file:///Users/username/folder/video.webm">
这显示了用户机器上的路径。否则会说什么呢? - Fishbite我曾经遇到过这个问题。 由于安全设置的限制,网站无法访问本地PC上的视频文件(这是可以理解的)。 唯一的解决方法是在本地PC上运行一个Web服务器(server2Go),然后将网页中所有与视频文件相关的引用都改为localhost/video.mp4。
<div id="videoDiv">
<video id="video" src="http://127.0.0.1:4001/videos/<?php $videoFileName?>" width="70%" controls>
</div>
<!--End videoDiv-->
我尽力将Dimitar Bonev的答案简化。
<html>
<head>
<title>HTML5 local video file player example</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>HTML5 local video file player example</h1>
<input type="file" accept="video/*"><br>
<video controls></video>
<script type="text/javascript">
(function localFileVideoPlayer() {
'use strict'
var playSelectedFile = function(event) {
var file = this.files[0]
var URL = window.URL || window.webkitURL
var fileURL = URL.createObjectURL(file)
var videoNode = document.querySelector('video')
videoNode.src = fileURL
}
var inputNode = document.querySelector('input')
inputNode.addEventListener('change', playSelectedFile, false)
})()
</script>
<p>I hereby signed confess solemnly that I have no idea what this code does. But it now works.
<p>Firefox Lubuntu 18.03
<p>Simplified: `http://jsfiddle.net/dsbonev/cCCZ2/` `https://stackoverflow.com/users/691308/dimitar-bonev`
</body>
</html>