验证视频上传无效

时间:2015-07-08 13:49:58

标签: javascript php validation

我允许我的网站访问者上传视频文件。但我想限制视频的大小和类型。我使用下面的代码进行验证,但它根本不起作用。

      function validation(thisform)
         {
            with(thisform)
         {
               if(validateFileExtension(file, "valid_msg", "Only Videos are allowed!",
              new Array("mp4","flv")) == false)
              {
                    return false;
          }
                 if(validateFileSize(file,6291456, "valid_msg",        "Document size should be less than 6MB !")==false)
             {
             return false;
               }
            }
               }
          </script>  




          <form class="form-horizontal" role="form" action="" method="POST" enctype="multipart/form-data" name="form" onsubmit="return validation(this)">
           <label for="" class="col-sm-5 control-label">Upload Your Video</label>
           <div class="col-sm-5">
          <input type="file" name="video"  id="i_file"> 
           <div id="valid_msg"/>
           <input type="submit" class="btn btn-primary" value="Save my Profile" name="register" style="background-color:#31B9A0; border:1px solid #fff; font-weight:bold;" id="B1"/>

1 个答案:

答案 0 :(得分:1)

使用JS,执行以下操作:

resp = JSON.parse(your_json_response);

使用jQuery,您可以执行以下操作:

document.getElementById("i_file").addEventListener("change", function () {
    var file = this.files[0];

    if (file) {
        var mbSize = file.size / 1024 / 1024;
        var fileIsMp4 = (file.type === "video/mp4");

        if (mbSize > 1 || !fileIsMp4) {
            alert("failure");
        } else {
            alert("success");
        }
    }
});