29

Codeigniterでファイルをアップロードするためにuploadifyを使用しています。ファイルをアップロードする前に、ファイル拡張子が正しいかどうかを確認する必要があります。http://jquery.bassistance.de/http://forum.jquery.com/で試しました

バーは適切な結果を得られませんでした。どうすれば同じことができるか教えてください。

前もって感謝します...

4

9 に答える 9

43

プラグインなしで実行したい場合は、次を使用できます。

Javascript、jQuery を使用:

$(document).ready( function (){
    $("#your_form").submit( function(submitEvent) {

        // get the file name, possibly with path (depends on browser)
        var filename = $("#file_input").val();

        // Use a regular expression to trim everything before final dot
        var extension = filename.replace(/^.*\./, '');

        // Iff there is no dot anywhere in filename, we would have extension == filename,
        // so we account for this possibility now
        if (extension == filename) {
            extension = '';
        } else {
            // if there is an extension, we convert to lower case
            // (N.B. this conversion will not effect the value of the extension
            // on the file upload.)
            extension = extension.toLowerCase();
        }

        switch (extension) {
            case 'jpg':
            case 'jpeg':
            case 'png':
                alert("it's got an extension which suggests it's a PNG or JPG image (but N.B. that's only its name, so let's be sure that we, say, check the mime-type server-side!)");

            // uncomment the next line to allow the form to submitted in this case:
//          break;

            default:
                // Cancel the form submission
                submitEvent.preventDefault();
        }

  });
});

HTML:

<form id="your_form" method="post" enctype="multipart/form-data">
    <input id="file_input" type="file" />
    <input type="submit">
</form>
于 2013-02-13T11:51:45.933 に答える
22

回答を投稿した人に感謝したいのですが、彼は投稿を削除しました。このようにできます。

$("#yourElem").uploadify({
   'uploader': ...,
   'script': ...
    'fileExt' : '*.jpg;*.gif;', //add allowed extensions
    .....,
    'onSelect': function(e, q, f) {
        var validExtensions = ['jpg','gif']; //array of valid extensions
        var fileName = f.name;
        var fileNameExt = fileName.substr(fileName.lastIndexOf('.') + 1);
        if ($.inArray(fileNameExt, validExtensions) == -1){
           alert("Invalid file type");
           $("#yourElem").uploadifyCancel(q);
           return false;
        }
    }
});

答えてくれてありがとう、それは本当にうまくいきました...

于 2013-02-13T11:41:39.087 に答える
3

次のコードでは、gif、png、jpg、jpeg、および bmp ファイルをアップロードできます。

var extension = $('#your_file_id').val().split('.').pop().toLowerCase();

if($.inArray(extension, ['gif','png','jpg','jpeg','bmp']) == -1) {
    alert('Sorry, invalid extension.');
    return false;
}
于 2017-04-23T17:02:48.193 に答える