2016-11-17 160 views
2

你好,我有以下代码:的Javascript获取视频长度

  <form action="postvideophp.php" method="post" enctype="multipart/form-data">  
     <input id="videoage" type="file" name="video" style="-webkit-appearance: none;-webkit-border-radius: 0;margin-left:-242px;margin-top:10px;opacity:0;"> 
     <label for="videoage" id="labelvideo">Choose Video...</label> 

     <textarea id="postTitleStyle" onkeyup="countChars('postTitleStyle','titlecount');" onkeydown="countChars('postTitleStyle','titlecount');" onmouseout="countChars('postTitleStyle','titlecount');" name="title" rows="1" maxlength = "180" placeholder="Title"><?php echo $title;?></textarea> 
     <a id="titlecount" style="color:#F52025;Font-family:Arial Black;display:table;margin-top:-20px;margin-left:840px;">0</a> 
     <textarea id="postTagStyle" onkeyup="countChars2('postTagStyle','descripcount');" onkeydown="countChars2('postTagStyle','descripcount');" onmouseout="countChars2('postTagStyle','descripcount');" name="desc" rows="2" maxlength = "1000" placeholder="Description"><?php echo $DESC;?></textarea> 
     <a id="descripcount" style="color:#3FDA21;Font-family:Arial Black;display:table;margin-top:-20px;margin-left:840px;">0</a>  
     <center><input type="submit" class = "Post2" value="[&nbsp;&nbsp;Post&nbsp;&nbsp;]"></center> 
    </form> 

这里是我的一些PHP代码它被张贴后:

if(FileSize($_FILES["video"]["tmp_name"]) >= 120){ 
    $uploadable = false; 
    header("Location:post-video"); 
    $_SESSION["FLAW"] = '10'; 

} 

我想要的浏览器访问错误如果发布的视频的文件大小大于120个字节(例如),则称为后视频。我现在遇到的问题是它会发布整个视频,如果用户上传一个非常大的视频文件,可能需要10分钟。视频发布后,它大于120bytes,显示错误屏幕。有没有办法,可能使用JavaScript来尽早检测视频的大小?如果是这样,我该如何快速做到这一点?

+0

你可以添加一些JavaScript来检查这个: 'var file = d 。ocument.getElementById( “videoage”)文件[0]; if(file.size> 120) {/ *做些什么* /}' – vbguyny

+0

@vbguyny感谢它的工作回答,所以你可以得到信用 – user7133318

+0

谢谢。您应该能够将我的评论标记为答案。 – vbguyny

回答

1

您可以添加一些JavaScript检查此:

var file = document.getElementById("videoage").files[0]; 
if (file.size > 120) { /* do something */ } 
+0

再次感谢;) – user7133318

1

可以

if ($_FILES["video"]['size'] >= $fileLimit) { 
    // handle oversized file 
} 

您可以更快的错误在支持的浏览器处理这个客户端使用通过$_FILES超级全球获得文件大小:

var fileInput = document.getElementById("videoage"); 
fileInput.addEventListener("change", function() { 
    if (fileInput.files[0].size > 120) { 
    alert("file too big"); 
    } 
}); 
+0

这是PHP,而不是javascript –

+1

更新后再添加javascript解决方案 – Victory