programing

업로드 시 업로드 수 제한

kingscode 2023. 6. 10. 16:15
반응형

업로드 시 업로드 수 제한

제 고객이 오래된 클래식 프레스 버전을 사용하고 있습니다. 여기 제가 찾은 github repo가 있지만 그가 사용하는 것은 훨씬 더 오래되었습니다.최신 Wordpress 버전을 실행하고 있습니다.일부 오래된 버전은 테마에서 버전을 찾을 수 없는 업로드와 함께 제공됩니다.여기 Functions.php, 여기 업로드가 있습니다.여기 제 페이지의 html이 있습니다. 볼 필요는 없지만, 페이지가 보호되어 있기 때문에 당신이 원한다면 전체 코드를 검사할 수 있는 유일한 방법입니다.

여러 장의 사진을 동시에 업로드할 수 있는 기능을 추가하고 싶습니다. 이 기능을 기능에 추가합니다.php

add_filter('appthemes_plupload_config', 'enable_plupload_multisel', 10 ,1);
function enable_plupload_multisel($app_plupload_config){
$app_plupload_config['plupload']['multi_selection'] = true;
return $app_plupload_config; }

그런데 나는 사용자가 8장 이상의 사진을 올리는 것을 어떻게 막을 수 있는지 모르겠어요?추가해봤습니다.max_files그리고.max_files_count그리고.max_file_count그리고 아무 것도 작동하지 않았고, 심지어 플러그인 자체의 소스 코드와 Js를 수정했지만 아무 것도 작동하지 않았습니다.나는 사용자가 8개 이상의 이미지를 업로드할 수 없도록 하고 싶습니다.

업로드를 포기한 후 제이쿼리를 사용하여 해보았지만 다시 작동하지 않았습니다.

 /* prevent form submission if user selects more than 8 pics */
 jQuery('#app-attachment-upload-pickfiles').change(function() {
     if (this.files.length > 8) {
         alert('Uploading more than 8 images is not allowed');
         this.value = '';
     }
 });
 // Prevent submission if limit is exceeded.
 jQuery('#mainform').submit(function() {
     if (this.files.length > 8) {
         jQuery('#app-attachment-upload-pickfiles').hide();
         jQuery('#step1').hide();
         return false;
     } else {
         jQuery('#app-attachment-upload-pickfiles').show();
         jQuery('#step1').show();
     }
 });

편집

여기 내 플로드 js. FilesAdded

    attachUploader.bind('FilesAdded', function(up, files) {
        jQuery.each(files, function(i, file) {
            jQuery('#app-attachment-upload-filelist').append(
                '<div id="' + file.id + '" class="app-attachment-upload-progress">' +
                file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' +
                '</div>');

            window.appFileCount += 1;
            APP_Attachment.hideUploadBtn();
        });

        up.refresh();
        attachUploader.start();
    });

그렇게 보이도록 수정했습니다.

    attachUploader.bind('FilesAdded', function(up, files) {
        var maxfiles = 8;
            if(up.files.length > maxfiles )
             {
                up.splice(maxfiles);
                alert('no more than '+maxfiles + ' file(s)');
             }
            if (up.files.length === maxfiles) {
                $('#app-attachment-upload-filelist').hide("slow"); // provided there is only one #uploader_browse on page
            }
        jQuery.each(files, function(i, file) {
            jQuery('#app-attachment-upload-filelist').append(
                '<div id="' + file.id + '" class="app-attachment-upload-progress">' +
                file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' +
                '</div>');

            window.appFileCount += 1;
            APP_Attachment.hideUploadBtn();
        });

        up.refresh();

        attachUploader.start();
    });

그게 다예요?이제 되나요?오류가 발생할 수 있기 때문에 테스트하지 않았습니다.

확실하지는 않지만 당신의 코드는 거의 작동할 것입니다.나는 당신이 수동으로 대기열에서 파일을 제거해야 한다고 생각합니다.removeFile방법.

이 코드를 사용해 보십시오.

    attachUploader.bind('FilesAdded', function(up, files) {
        var maxfiles = 8;

        // remove all new files after the max of files
        jQuery.each(up.files, function(i, file) {
            if(i > maxfiles){
                up.removeFile(file);
            }
        });      
});

언급URL : https://stackoverflow.com/questions/46917999/limit-the-number-of-uploads-in-plupload

반응형