0

我已经建立了一个非常简单的原型来测试 YUI 上传器。(flash 版本)该文件正在发送到发送简单 Ajax 响应的服务器。但是,唯一被触发的事件是fileselectand uploadstartuploadcomplete, uploaderror, 并且uploadprogress永远不会被触发。这是 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;
}

我在这里做错了什么?

4

2 回答 2

0

您可能有“相同的域策略”问题。上传的目标应该和swfuploader.swf的源一样

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

另请注意有关 IE 中的 Flash 错误的备注,您可以通过将随机参数附加到您的 swfuploader url 来修复它。

[编辑:]我在我的服务器上测试了你的文件,虽然它看起来完全没问题,但在这里也失败了。即使是 uploadstart 事件也不会随机触发。似乎是 YUI 3.5.1 中的一个错误。

解决方法是使用 uploader-deprecated 的 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

于 2012-05-17T11:10:23.020 回答
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' });
});
于 2012-06-13T18:40:00.400 回答