简体   繁体   English

如何将Plupload与Node.js一起使用并显示上传的百分比?

[英]How do I use Plupload with Node.js and show the percentages of upload?

Currently, my code works. 目前,我的代码有效。 However, when a file is uploaded, no percentages are sent back to the javascript code. 但是,上传文件时,不会将百分比发送回javascript代码。 (I guess my server needs to send the chunk percentage back?) (我想我的服务器需要发回块百分比?)

The "UploadProgress" event just prints "0" when it's complete. “UploadProgress”事件在完成时只打印“0”。

<script>
    $(function(){
        $("#button_holder").show();
        var uploader = new plupload.Uploader({
            runtimes : 'html5,flash,silverlight,html4',
            browse_button : 'pickfiles',
            container : 'button_holder',
            multi_selection: true,
            url : '/upload',
            flash_swf_url : '/js/plupload/js/Moxie.swf',
            silverlight_xap_url : '/js/plupload/js/Moxie.xap',
        });

        uploader.bind('FilesAdded', function(up, files) {
            $("#button_holder").hide(); 

            plupload.each(files, function(file) {
                var item = '<div class="upload_item" id="' + file.id + '">' + '<b class="percent"></b></div>' + file.name + ', ' + plupload.formatSize(file.size) + '</div>'
                $("#progress_holder").append(item);
            }); 

            uploader.start();
            return false;
        });

        uploader.bind('FileUploaded', function(uploader, file, response){
            if(response.status == 200){ 
                var icon = "<i class='fa fa-check fa-fw'></i>";
            }else{
                var icon = "<i class='fa fa-times fa-fw'></i>";
            }
            var html = '<div class="success_item">' + icon + ' ' + file.name + '</div>';
            $("#progress_holder").append(html);
        });

        uploader.bind('UploadComplete', function(uploader, files){
        });

        uploader.bind('UploadProgress', function(up, file) {
            console.log(file.percent); //just says "0"
            $("#" + file.id).first(".percent").html(file.percent + "%");
            return false;
        });
        uploader.init();
    });
</script>

This is my backend code: 这是我的后端代码:

var express = require('express');
var Pluploader = require('node-pluploader');
var ejs = require('ejs');
var bodyParser = require('body-parser')
var request = require('request');
var http = require('http');


var app = express();
app.set('views','/home/user/heaven/templates');
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true}));
app.use(bodyParser.json());
app.use(express.static('/home/user/heaven/media'));



var pluploader = new Pluploader({
  uploadLimit: 100, //MB
  uploadDir: '/home/user/heaven/uploads'
});

pluploader.on('fileUploaded', function(file, req) {
  console.log(file);
});
pluploader.on('error', function(error) {
    throw error;
});

app.post('/upload', function(req, res){
    pluploader.handleRequest(req, res);
});

app.get('/', function (req, res) {
    res.render('index', {});
});

var server = app.listen(3000, function () {
    console.log('Listening to server.');
});

Just change url to: 只需将网址更改为:

url : 'http://127.0.0.1:3000/upload'

and instead of binding UploadProgress later, do it in init, like this: 而不是以后绑定UploadProgress ,在init中执行,如下所示:

var uploader = new plupload.Uploader({
            runtimes: 'html5,flash,silverlight,html4',
            browse_button: 'pickfiles',
            container: 'button_holder',
            multi_selection: true,
            url: 'http://127.0.0.1:3000/upload',
            flash_swf_url: '/js/plupload/js/Moxie.swf',
            silverlight_xap_url: '/js/plupload/js/Moxie.xap',
            init: {
                UploadProgress: function(up, file) {
                    console.log('file%: '+file.percent);

                }
            }
        });

I tested, it works in your code. 我测试过,它适用于您的代码。 The same thing doesn't work with bind, which is quite weird. 同样的事情不适用于绑定,这很奇怪。 I'll figure it out later. 我稍后会弄清楚的。

Server code: https://github.com/aishwat/plupload_server 服务器代码: https//github.com/aishwat/plupload_server

Client code: https://github.com/aishwat/plupload_client/blob/master/examples/custom.html 客户代码: https//github.com/aishwat/plupload_client/blob/master/examples/custom.html

And if you don't want to use Plupload, there's another way using XMLHttpRequest progress event, something like: 如果您不想使用Plupload,还有另一种使用XMLHttpRequest进度事件的方法,例如:

xhr.upload.addEventListener("progress", function (evt) {});

Here's the node.js code using this approach (written by Rohit Kumar ). 这是使用这种方法node.js代码 (由Rohit Kumar编写)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM