1

我正在尝试让 Plupload 的 html4 运行时与 S3 一起使用。不幸的是,我在这里找到的示例必须是一个非常旧的 plupload 版本或没有正确记录,并且没有实时工作示例或源代码下载。

所以我将我的 Plupload/Flash 运行时的工作版本带到 S3 并简单地将 html4 运行时而不是 flash 并添加'success_action_redirect': (我从 plupload 论坛了解到,plupload 需要这样做才能知道文件上传何时完成)。我发现它确实有效,但有些脚本确实失败了。

这是我的源代码...

PHP:

<?php
$bucket = 'MyBucket';
$accessKeyId = 'MyKey';
$secret = 'MySecret';

// Policy Setup
if (!function_exists('hash_hmac')) : function hash_hmac($algo, $data, $key, $raw_output = false){
    $blocksize = 64;
    if (strlen($key) > $blocksize)
        $key = pack('H*', $algo($key));

    $key = str_pad($key, $blocksize, chr(0x00));
    $ipad = str_repeat(chr(0x36), $blocksize);
    $opad = str_repeat(chr(0x5c), $blocksize);
    $hmac = pack('H*', $algo(($key^$opad) . pack('H*', $algo(($key^$ipad) . $data))));

    return $raw_output ? $hmac : bin2hex($hmac);
}
endif;
// prepare policy
$policy = base64_encode(json_encode(array(
    'expiration' => date('Y-m-d\TH:i:s.000\Z', strtotime('+1 day')),  
    'conditions' => array(
        array('bucket' => $bucket),
        array('acl' => 'authenticated-read'),
        array('success_action_redirect' => 'http://mydomain.com/ThisCanBeAnyPage.php'),
        array('starts-with', '$key', ''),
        array('success_action_status' => '201'),
        array('starts-with', '$name', ''),  
        array('starts-with', '$Filename', ''), 
    )
)));
// Sign Policy
$signature = base64_encode(hash_hmac('sha1', $policy, $secret, true));
?>

Javascript:

<script type="text/javascript" src="js/plupload.full.js"></script>
<script type="text/javascript" src="js/plupload.html4.js"></script>
<script type="text/javascript">
// Custom example logic
$(function() {
    var uploader = new plupload.Uploader({
        preinit : {
            UploadFile: function(up, file) {
        // When file is loaded, directory on S3 is set here.
                up.settings.multipart_params.key = 'myFolder/'+file.name;
            }
        },
        runtimes : 'html4',
        browse_button : 'pickfiles',
        container : 'container',
        url : 'http://<?php echo $bucket; ?>.s3.amazonaws.com/',
        max_file_size : '300mb',
        multipart: true,
        multipart_params: {
            'key': '${filename}', // use filename as a key
            'success_action_redirect':'http://mydomain.com/ThisCanBeAnyPage.php',
            'Filename': '${filename}', // adding this to keep consistency across the runtimes
            'acl': 'authenticated-read',
            'success_action_status': '201',
            'AWSAccessKeyId' : '<?php echo $accessKeyId; ?>',       
            'policy': '<?php echo $policy; ?>',
            'signature': '<?php echo $signature; ?>'
        },
        file_data_name: 'file',
        multiple_queues: true,
        filters : [
            {title : "Image Files", extensions : "jpg,jpeg"},
        ],
        flash_swf_url : 'js/plupload.flash.swf',
        silverlight_xap_url : 'js/plupload.silverlight.xap'
    });

    uploader.bind('Init', function(up, params) {
        //$('#filelist').html("<div>Current runtime: " + params.runtime + "</div>");
    });

    $('#uploadfiles').click(function(e) {
        uploader.start();
        e.preventDefault();
    });

    uploader.bind('FilesAdded', function(up, files) {
        $.each(files, function(i, file) {
        // file.name seems to be the only thing that is returned so file.size is displayed as 'N/A'
            $('#filelist').append(
                '<div class="Section" id="'+file.id+'">'
                    +file.name+' ('+plupload.formatSize(file.size)+')&nbsp;&nbsp;<b>'
                    +'</b><div class="PlupLoadingBarWrap"><div class="PlupLoadingBar"></div></div>'
                +'</div>'
            );
        });
        up.refresh(); // Reposition Flash/Silverlight
    });

    uploader.bind('UploadProgress', function(up, file) {
        // file.name seems to be the only thing that is returned
        // This triggers when file is complete before 'FileUploaded' and not during upload process.
    });

    uploader.bind('Error', function(up, err) {
        // Haven't received an error to know if this works.
        $('#filelist').append("<div>Error: " + err.code +
            ", Message: " + err.message +
            (err.file ? ", File: " + err.file.name : "") +
            "</div>"
        );

        up.refresh(); // Reposition Flash/Silverlight
    });

    uploader.bind('FileUploaded', function(up, file) {
        // file.name seems to be the only thing that is returned
        // This does trigger once file is completely loaded
    });

    uploader.bind('UploadComplete', function(up, files){
        // Calls a function
    });

    uploader.init();
});
</script>

HTML:

<h3>Custom example</h3>
<div id="container">
    <div id="filelist">No runtime found.</div>
    <br />
    <a id="pickfiles" href="#">[Select files]</a>
    <a id="uploadfiles" href="#">[Upload files]</a>
</div>

所以总而言之,这个脚本确实有效,但缺少 file.size 并且根据我能够找到的内容没有进度指示。我想知道那里是否有一个 plupload html4 脚本,其中包含这里缺少的东西。我有一个尚未测试的解决方法。但我会保持更新。

4

1 回答 1

0

简单的答案是否定的。

HTML4 本身不支持任何这些功能。

但是,如果您使用提交功能,您可以做到这一点。基本上,您需要在客户端设计自己的分块,使用 post 提交块并控制块的数量以制作进度条。它在理论上很简单,但在实践中很难看。为什么可以使用闪光灯或银光注射?

于 2012-07-11T16:03:07.843 回答