Processing PLUpload uploads with node.js

我是研究僧i 提交于 2019-12-06 14:05:26

I have no problem to use plupload(in HTML5 mode) with node.js with below code:

module.exports.savePhoto= (req, res) ->
  if req.url is "/upload" and req.method.toLowerCase() is "post"
    console.log 'savePhoto: req.url=', req.url, 'req.method=', req.method
    form = new formidable.IncomingForm()
    files = []
    fields = []
    form.uploadDir = config.PATH_upload
    form.on("field", (field, value) ->
      console.log field, value
      fields.push [ field, value ]
    ).on("file", (field, file) ->
      console.log field, file
      files.push [ field, file ]
    ).on "end", ->
      console.log "-> upload done: fields=", fields
      console.log "received fields:", util.inspect(fields)
      console.log "received files:",  util.inspect(files)
      size = files[0][1].size
      pathList = files[0][1].path.split("/")
      #console.log 'pathList=', pathList
      file = pathList[pathList.length - 1]
      console.log "file=" + file
      ......

I created node-pluploader to handle this, as I found elife's answer didn't work for chunked uploads, as said chunks come in on different requests.

Express-based usage example:

var Pluploader = require('node-pluploader');

var pluploader = new Pluploader({
  uploadLimit: 16
});

 /*
  * Emitted when an entire file has been uploaded.
  *
  * @param file {Object} An object containing the uploaded file's name, type, buffered data & size
  * @param req {Request} The request that carried in the final chunk
  */
pluploader.on('fileUploaded', function(file, req) {
  console.log(file);
});

/*
  * Emitted when an error occurs
  *
  * @param error {Error} The error
  */
pluploader.on('error', function(error) {
    throw error;
});

// This example assumes you're using Express
app.post('/upload', function(req, res){
  pluploader.handleRequest(req, res);
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!