2013-04-26 2 views
1
$('INPUT[type="file"]').change(function() { 
var ext = this.value.match(/\.(.+)$/)[1]; 
var control = $("#uploaded_file"); 
switch (ext) { 
    case 'doc': 
    case 'docx': 
    case 'pdf': 
    case 'wps': 
    case 'rtf': 
    case 'txt': 
    case 'xps': 
     $('#uploadButton').attr('disabled', false); 
     break; 
    default: 
     alert('\'' + ext + '\' is not an allowed file type.\n\nPlease select the correct file type:\n\n\t.DOC, .DOCX, .PDF, .WPS, .RTF, .TXT, .XPS'); 
     $('#uploadButton').attr('disabled', true); 
     control.replaceWith(control = control.val('').clone(true)); 
    } 
}); 

이제는 사용자가 ".TXT"가있는 파일을 선택하면 TXT가 txt와 같지 않으므로 예외 오류가 발생하므로 사용하려고 시도했습니다. strtolower 함수. 그러나 스크립트를 사용하면 스크립트 자체가 작동하지 않습니다.파일 jquery에서 파일 유형을 확인하십시오

이 작동하지 않습니다하여 strtolower와 스크립트를 수정 :

$('INPUT[type="file"]').change(function() { 
var ext = this.value.match(/\.(.+)$/)[1]; 
    var cext = strtolower(ext); 
var control = $("#uploaded_file"); 
switch (cext) { 
    case 'doc': 
    case 'docx': 
    case 'pdf': 
    case 'wps': 
    case 'rtf': 
    case 'txt': 
    case 'xps': 
     $('#uploadButton').attr('disabled', false); 
     break; 
    default: 
     alert('\'' + cext + '\' is not an allowed file type.\n\nPlease select the correct file type:\n\n\t.DOC, .DOCX, .PDF, .WPS, .RTF, .TXT, .XPS'); 
     $('#uploadButton').attr('disabled', true); 
     control.replaceWith(control = control.val('').clone(true)); 
    } 
}); 

어디 작동하지 않는 원인이되는 오류?

+2

'하여 strtolower()을'PHP는 기능이 아닌 자바 스크립트 기능입니다. String.toLowerCase() (예 :'var ext = "TXT"; switch (ext.toLowerCase()) {...')를 찾고있다. –

답변

3

나는 strtolower()이 자바 스크립트 기능이라고 생각하지 않습니다. string.toLowerCase()

var cext = ext.toLowerCase(); 

Live sample

문서보십시오 : https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/toLowerCase

+1

네, 참고할만한 정보가 적은 소스로 연결하는 것이 좋습니다. W3Schools는 기본적으로 [다양한 이유로 좋아하지 않습니다] (http://w3fools.com/)입니다. [Mozilla의 개발자 네트워크 설명서] (https://developer.mozilla.org/en-US/docs/CSS)를 사용해보십시오. –

+0

@DavidThomas, 지적했다. –

+1

죄송합니다. 저는이 코딩에 너무 빠져있었습니다 ... 나는 PHP와 자바 스크립트를 혼합했습니다 ... 롤 ... 나는 오류를 봅니다! 감사 – Si8

관련 문제