Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js uploading with PUT

Tags:

node.js

i've seen (few) examples of uploading servers in node.js, using multipart or formidable lib, dealing with http post.

but, if i want to use PUT to upload file, something as

% curl -T file.tar.gz http://node:8000/upload/

can you point me to some examples? how i read the request body?

greets

like image 319
cetto laqualunque Avatar asked Dec 09 '25 23:12

cetto laqualunque


1 Answers

Maybe I'm wrong, but If you take the formidable example replacing 'post' for 'put' should work.

var formidable = require('formidable'),
    http = require('http'),
    sys = require('sys');

http.createServer(function(req, res) {
  if (req.url == '/upload' && req.method.toLowerCase() == 'put') {
    // parse a file upload
    var form = new formidable.IncomingForm();
    form.parse(req, function(err, fields, files) {
      res.writeHead(200, {'content-type': 'text/plain'});
      res.write('received upload:\n\n');
      res.end(sys.inspect({fields: fields, files: files}));
    });
    return;
  }

  // show a file upload form
  res.writeHead(200, {'content-type': 'text/html'});
  res.end(
    '<form action="/upload" enctype="multipart/form-data" method="post">'+
    '<input type="text" name="title"><br>'+
    '<input type="file" name="upload" multiple="multiple"><br>'+
    '<input type="submit" value="Upload">'+
    '</form>'
  );
});
like image 132
masylum Avatar answered Dec 11 '25 13:12

masylum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!