2016-11-17 61 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