将plupload集成到我的媒体插件中

时间:2015-09-23 04:35:26

标签: php wordpress file-upload image-uploading plupload

我使用buddypress activity plus作为媒体插件。该插件使用valums-file-upload上传媒体,我想添加plupload并删除valums-file-upload。到目前为止我做了什么: 删除了这一部分,

var uploader = new qq.FileUploader({
        "element": $('#med_tmp_photo')[0],
        //"listElement": $('#med_tmp_photo_list')[0],
        "allowedExtensions": ['jpg', 'jpeg', 'png', 'gif'],
        //"action": ajaxurl,
        "params": {
            "action": "med_preview_photo"
        },

        "onComplete": createPhotoPreview,
        template: '<div class="qq-uploader">' +
            '<div class="qq-upload-drop-area"><span>' + broadcast.drop_files + '</span></div>' +
            '<div class="qq-upload-button">' + broadcast.upload_file + '</div>' +
            '<ul class="qq-upload-list">' +
         '</div>'
    });

并补充说,

var uploader = new plupload.Uploader({
            runtimes : 'html5,flash,silverlight', // Set runtimes, here it     will use HTML5, if not supported will use flash, etc.
            browse_button : 'pickfiles', // The id on the select files button
            multi_selection: false, // Allow to select one file each time
            container : 'uploader', // The id of the upload form container
            max_file_size : '800mb', // Maximum file size allowed
            url : //'upload.php', // The url to the upload.php file

             resize: {width: 300,height: 300,quality: 70},

            flash_swf_url : 'js/plupload.flash.swf', // The url to thye flash file
            silverlight_xap_url : 'js/plupload.silverlight.xap', // The url to the silverlight file
            filters : [ {title : "Image files", extensions : "jpg,gif,png,jpeg"} ] // Filter the files that will be showed on the select files window

    });

        // RUNTIME
        uploader.bind('Init', function(up, params) {
            $('#runtime').text(params.runtime);
        });

        // Start Upload ////////////////////////////////////////////
        // When the button with the id "#uploadfiles" is clicked the upload will start
        $('#med_submit').click(function(e) {
            uploader.start();
            e.preventDefault();
});

并将file_uploader.php更改为以下内容,

class plupload_1 {
/**
 * Save the file to the specified path
 * @return boolean TRUE on success
 */
function save($path) {
    $input = fopen("php://input", "r");
    $temp = tmpfile();
    $realSize = stream_copy_to_stream($input, $temp);
    fclose($input);

    $target = fopen($path, "w");
    fseek($temp, 0, SEEK_SET);
    stream_copy_to_stream($temp, $target);
    fclose($target);

    return true;
}

}

/**
 * Handle file uploads via regular form post (uses the $_FILES array)
 */

class plupload_2 {

function __construct(){

        $this->file = new plupload_1();

}

最后更改了'class_bpfb_binder.php'中的'ajax_preview_photo'

从此,

function ajax_preview_photo () {
    $dir = BPFB_PLUGIN_BASE_DIR . '/img/';
    require_once(BPFB_PLUGIN_BASE_DIR . '/lib/external/file_uploader.php');
    $uploader = new qqFileUploader(array('jpg', 'jpeg', 'png', 'gif'));
    $result = $uploader->handleUpload(BPFB_TEMP_IMAGE_DIR);
    //header('Content-type: application/json'); // For some reason, IE doesn't like this. Skip.
    echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
    exit();
}

到此,

  function ajax_preview_photo () {
    //$dir = BPFB_PLUGIN_BASE_DIR . '/img/';
    require_once(BPFB_PLUGIN_BASE_DIR . '/lib/external/file_uploader.php');
    $uploader = new plupload_2();
    $result = $uploader->PlupLoad(BPFB_TEMP_IMAGE_DIR);
    //header('Content-type: application/json'); // For some reason, IE doesn't like this. Skip.
    echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
    exit();
} 

但我不确定我在做什么是正确的,或者如果有不同的方法,我们将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:1)

如果您不介意,请使用此加载代码。我总是在wordpress插件中使用这个代码,这是非常简单和干净的代码。 http://www.krishnakantsharma.com/image-uploads-on-wordpress-admin-screens-using-jquery-and-new-plupload/#.VgIs1fRLjIU

尝试一下,如果有任何疑问,请告诉我。