YUI 3 UploaderFlash不会触发'上传'事件

时间:2012-05-09 18:07:31

标签: flash yui3 yui-uploader

我已经设置了一个非常简单的原型来测试YUI Uploader。 (flash版本)该文件正在向发送简单Ajax响应的服务器发送。但是,触发的唯一事件是fileselectuploadstart。永远不会触发uploadcompleteuploaderroruploadprogress。这是YUI 3.5.1。

HTML和JS

<!DOCTYPE html>
<html>
<head>
    <title>Uploader Bizness</title>
    <script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script src="http://yui.yahooapis.com/3.5.1/build/yui/yui-min.js"></script>
</head>
<body>
    <div id="upload"></div>
    <div id="progress"></div>
    <script>
        'use strict';
        YUI().use('uploader', 'uploader-flash', function (Y) {
            Y.Uploader = Y.UploaderFlash;
            var uploader = new Y.Uploader({
                width: '150px',
                height: '40px',
                swfURL: '/flashuploader.swf'
            }).render('#upload');

            uploader.on('fileselect', function (G) {
                var firstFile = G.fileList[0];
                uploader.upload(firstFile, 'http://localhost:28107/home/upload', { postvar1: 'foo', postvar2: 'bar' });
            });

            uploader.on('uploadstart', function () {
                console.log('Upload started');
            });

            uploader.on('uploadcomplete', function (e) {
                console.log('Upload completed successfully', e);
            });

            uploader.on('uploaderror', function (e) {
                console.log('Upload error', e);
            });

            uploader.on('uploadprogress', function (file, bytesloaded, bytesTotal, percentLoaded, e) {
                $('#progess').html(percentLoaded);
            });
        });
    </script>
</body>
</html>

服务器端代码

public JsonResult Upload()
{
    var result = new JsonResult();
    result.Data = new {message = "great success"};
    return result;
}

我在这里做错了什么?

2 个答案:

答案 0 :(得分:0)

您可能遇到“相同的域策略”问题。上传的目标应与swfuploader.swf

的源相同

您的上传目标使用端口28107;是您的页面和从同一端口或默认HTTP端口提供的swfuploader.swf?如果不是,您需要确保它们是或在您的服务器上放置一个crossdomain.xml文件。有关如何编写一个的说明,请参阅http://yuilibrary.com/yui/docs/uploader/

另请注意有关IE中Flash错误的说法,您可以通过在swfuploader网址中附加随机参数来解决此问题。

[编辑:] 我在我的服务器上测试了你的文件,虽然它看起来非常好,但它也失败了。甚至uploadstart事件也不会随机发生。似乎是YUI 3.5.1中的错误。

解决方法是使用不支持uploader的3.4.1上传器。我测试了这个版本并且有效:

<script>
'use strict';
YUI().use('uploader-deprecated', function (Y) {

    var uploader = new Y.Uploader({
        boundingBox: '#upload', // use boundingBox attribute instead of render('uploader')
        // width: '150px', set width & height using css 
        // height: '40px',
        swfURL: 'ENTER_PATH/uploader.swf',
        simLimit: 2
    }); // no .render('upload') !

    uploader.on('fileselect', function (G) {
        // var firstFile = G.fileList[0];
        // uploader.upload(firstFile, 'http://localhost:28107/home/upload', { postvar1: 'foo', postvar2: 'bar' });
        uploader.upload('file0', 'http://localhost:28107/stackupload.php', 'POST', { });
    });

    uploader.on('uploadstart', function () {
        console.log('Upload started');
    });

    uploader.on('uploadcomplete', function (e) {
        console.log('Upload completed successfully', e);
    });

    uploader.on('uploaderror', function (e) {
        console.log('Upload error', e);
    });
 /* not tested see below 
    uploader.on('uploadprogress', function (file, bytesloaded, bytesTotal, percentLoaded, e) {
        $('#progess').html(percentLoaded);
    });
*/
});

'uploadprogress'事件的事件签名也不同。我使用的代码:

    uploader.on('uploadprogress', function(event){
        var progress = Math.round(100 * event.bytesLoaded / event.bytesTotal);
        progressBar.set("progress", progress);
    });

您还需要自己设置样式。见http://yuilibrary.com/yui/docs/uploader-deprecated/index.html

答案 1 :(得分:0)

更改

<script src="http://yui.yahooapis.com/3.5.1/build/yui/yui-min.js"></script>

<script src="http://yui.yahooapis.com/3.6.0pr2/build/yui/yui-min.js"></script>

并订阅file对象上的事件而不是uploader对象:

uploader.on('fileselect', function (G) {
    var firstFile = G.fileList[0];
    firstFile.on('uploadstart', function (event) {
        console.log('Upload started');
        // var file = event.currentTarget;
    });
    uploader.upload(firstFile, 'http://localhost:28107/home/upload', { postvar1: 'foo', postvar2: 'bar' });
});