2013-09-23 98 views
2

我使用Jquery驗證器「http://jqueryvalidation.org/」驗證了我的表單。 我在驗證「<input type="file" />」字段時發生問題,其「required: true,」驗證正在運行,當我接受錯誤的文件類型時,會引發錯誤。但在「IE和Chrome」中,如果我選擇了正確的文件格式,它仍然會給出錯誤,儘管它在Firefox中正常工作。<input type =「file」/>文件格式驗證在Chrome和IE中不起作用

創造了小提琴太...這是怎麼回事正好爲我工作: http://jsfiddle.net/aasthatuteja/v6x8P/

**如果您在Chrome & IE檢查它會給這個問題,但如果你在Firefox檢查它會工作,並會提供「已提交」的aert!

enter image description here

請在以下提到的代碼: -

jQuery的

<script src="~/Scripts/js/jquery.validate.js"></script> 
<script src="http://jquery.bassistance.de/validate/additional-methods.js"></script> 

<script> 

    $().ready(function() { 

     // validate signup form on keyup and submit 
     $("#deploymentUploadForm").validate({ 

      rules:{ 
       File: { 
        required: true, 
        accept: "zip" 
       } 
      }, 

      messages:{ 
       File: { 
        required: "This field is mandatory!", 
        accept: "Accepts only zip file!" 
       } 
      } 

     }); 

    }); 
</script> 

HTML

<form action="~/Deployment/FileUpload" name="deploymentUploadForm" id="deploymentUploadForm" enctype="multipart/form-data" method="post"> 
    <h1>Deployment</h1> 
    <p> 
     <input type="file" name="File" accept="application/zip"> 
    </p> 
    <div role="button" class="marginTop50 marginBottom"> 
    <p> 
     <input type="submit" id="getDeploymentList" value="Upload" class="active" > 
    </p> 
    </div> 
</form> 

的onsubmit JQUERY

$("#getDeploymentList").click(function() { 
    if ($("#deploymentUploadForm").valid()) { 

     $("#deploymentUploadForm").submit(); 
     $('#stepSummary').empty(); 
     $.loader({ 
      className: "blue-with-image", 
      content: 'Please wait...Your request is being processed!' 
     }); 

    }; 
}); 

請讓我知道如果你需要任何其他信息。

感謝adavace!

+1

@TusharGupta:不,它們是一樣的,甚至是$(功能)... – dandavis

+0

@dandavis哦對不起,我知道了 –

+0

你在OS X上的Chrome 29中工作正常。你測試哪個IE?由於驗證使用HTML5文件API,因此它將在IE9及更低版本中失敗[請參閱(http://caniuse.com/#feat=fileapi))。 – insertusernamehere

回答

5

嘗試(HTML):

<input id="fileSelect" type="file" accept=".zip,application/octet-stream,application/zip,application/x-zip,application/x-zip-compressed" /> 

的jQuery(驗證)

accept: "application/octet-stream,application/zip,application/x-zip,application/x-zip-compressed" 

,並與jQuery插件(想法):

(function($) 
{ 
    $.fn.acceptFileType=function(types) 
    { 
     if (types == undefined) 
     { 
      return true; 
     }else{ 
      types = types.split(",") 
     } 
     this.each(function(){ 
      $(this).bind("change",function() 
      { 
       if(!$.inArray($(this).val().replace(/([\d\w.]+)(\.[a-z0-9]+)/i,'\2') , types)) 
       { 
        $(this).val(''); 
        return false; 
       } 
       return true; 
      }); 
     }); 
    }; 
})(jQuery); 
$(":file").acceptFileType(".zip"); 

如果你有一個PHP,看到這個回購

https://github.com/erlandsen/upload

祝您好運!

+0

謝謝!我只是改變了「接受:」application/octet-stream,application/zip,application/x-zip,application/x-zip-compressed「'並且它能夠工作。非常感謝! – UID

3

我能得到小提琴從拉鍊改變你接受的規則如下工作:

accept: "application/zip,application/octet-stream,application/x-zip,application/x-zip-compressed" 

它看起來像Firefox使用不同的MIME類型比IE或Chrome的zip文件。這爲zip文件使用了更廣泛的MIME類型列表。您需要決定它是否針對您的目的過於寬泛。

+0

謝謝!我只是改變了「接受:」application/octet-stream,application/zip,application/x-zip,application/x-zip-compressed「'並且它能夠工作。非常感謝! – UID

相關問題