2013-02-19 93 views
1

我正在试图为Wordpress创建一个插件插件,我正在使用Plupload上传一些图片。如何在Plupload中放弃文件上传?

在用户上传新图库的图像之前,必须添加图库名称。由于我使用拖放功能,因此如果图库名称为空,我需要中止文件上载。

我试过各种解决方案,但我无法停止文件上传(我认为)。

在我的SJ脚本的底部,我正在检查dragover。我想中止的地方标有//HERE FILE UPLOAD MUST ABORT

但我不确定这是否是正确的地方。任何人都可以告诉我可以在哪里放弃dragover上的上传(并放下)?

// PHP code - I'm adding this data here, because I need some WP data 
function plupload_init() { 

    //$image_size = get_option('sim_gallery_max_width_height'); 

    $plupload_arr = array(
     'runtimes' => 'html5,silverlight,flash,html4,browserplus,gears', 
     'browse_button' => 'plupload-browse-button', // will be adjusted per uploader 
     'container' => 'plupload-upload-ui', // will be adjusted per uploader 
     'drop_element' => 'drag-drop-area', // will be adjusted per uploader 
     'file_data_name' => 'async-upload', // will be adjusted per uploader 
     'multiple_queues' => true, 
     'max_file_size' => wp_max_upload_size() . 'b', 
     'url' => admin_url('admin-ajax.php'), 
     'flash_swf_url' => includes_url('js/plupload/plupload.flash.swf'), 
     'silverlight_xap_url' => includes_url('js/plupload/plupload.silverlight.xap'), 
     'filters' => array(array('title' => __('Image files'), 'extensions' => 'jpg,jpeg,gif,png')), 
     'multipart' => true, 
     'urlstream_upload' => true, 
     'multi_selection' => false, // will be added per uploader 
     // additional post data to send to our ajax hook 
     'multipart_params' => array(
      '_ajax_nonce' => "", // will be added per uploader 
      'action' => 'plupload_action', // the ajax action name 
      'imgid' => 0 // will be added per uploader 
     ) 
    ); 
?> 
    <script type="text/javascript"> 
     var plupload_config_vars = <?php echo json_encode($plupload_arr); ?>; 
    </script> 
<?php 
} 


// JS scrip 
function init_image_upload() { 

    if(jQuery('#image-upload').length == 0) 
     return; 

    var plupload_config = JSON.parse(JSON.stringify(plupload_config_vars)); 
    var uploader = new plupload.Uploader(plupload_config); 

    // Control gallery name 
    control_gallery_name(); 


    uploader.bind('Init', function(up){}); 

    uploader.init(); 

    // a file was added in the queue 
    uploader.bind('FilesAdded', function(up, files){ 
    }); 

    // Change border color on drop zone 
    function drop_area_visual_feedback(up) { 
     var uploaddiv = jQuery('#plupload-upload-ui'); 

     if (up.features.dragdrop) { 
      uploaddiv.addClass('drag-drop'); 

      jQuery('#drag-drop-area').bind('dragover.wp-uploader', function(){ // dragenter doesn't fire right :(
       if(!jQuery('input[name="gallery-name"]').val()) { 
        jQuery('label.missing-name').removeClass('hidden'); 

        //HERE FILE UPLOAD MUST ABORT 

       } else { 
        uploaddiv.addClass('drag-over'); 
       } 

      }).bind('dragleave.wp-uploader, drop.wp-uploader', function(){ 
        uploaddiv.removeClass('drag-over'); 
      }); 
     } else { 
      uploaddiv.removeClass('drag-drop'); 
      jQuery('#drag-drop-area').unbind('.wp-uploader'); 
     } 
    } 
} 

回答

1

你有没有试过把你的代码放入FilesAdded处理程序,像这样?

// a file was added in the queue 
uploader.bind('FilesAdded', function(up, files){ 
     if(!jQuery('input[name="gallery-name"]').val()) { 
       jQuery('label.missing-name').removeClass('hidden'); 
       up.splice(0); 
     } 
}); 
+0

谢谢。这就是诀窍:) – Steven 2013-02-20 20:31:07