简体   繁体   中英

Fileupload with Dropzone and Express: Response is undefined

I'm trying to implement a simple image upload demo with Dropzone and Express. My form looks like this:

<form id="ul-widget" action="/fileupload" class="dropzone" enctype="multipart/form-data">
 <div class="fallback">
   <input name="file" type="file" multiple />
 </div>
</form>

The javascript which belongs to this form looks like this:

<script type="text/javascript">
    Dropzone.options.ulWidget = {
        paramName: 'file',
        init: function() {
            this.on('complete', function( file, resp ){
              console.log(file);
              console.log(resp);
        });
      }
    }
</script>

As you can see right now I'm simply logging "file" and "resp", but "resp" is always 'undefined' even though the upload works. The backend code looks like this:

var express = require("express");
var app = express();
var multer = require("multer");
var upload = multer({ dest: 'uploads/' } );

app.set("view engine", "ejs");
app.use('/static', express.static(__dirname + '/node_modules/'));

app.get("/", function(req, res){
    res.render("main");
});

app.post("/fileupload", upload.single('file'), function(req, res, next){
    console.log(req.file);
    return res.status(200).send(req.file);
});

app.listen(8080, "0.0.0.0", function(){
   console.log("Dropzone Demo started."); 
});

The files are appearing in the upload folder, but the response to the front-end is allways 'undefined'. I worked with this article https://www.sitepoint.com/file-upload-form-express-dropzone-js/

The solution for the problem was just to replace 'complete' with 'success' and now it works. According to the Docs 'complete' doesn't have resp, while 'success' gives back two parameters (file, resp): http://www.dropzonejs.com/#event-success

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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