我在request's documentation page上读到,可以将文件流式传输到put或post请求,如下所示:

fs.createReadStream('file.json').pipe(request.put('http://example.com/obj.json'));
request.get('http://google.com/img.png').pipe(request.put('http://example.com/img.png'));

但似乎没用。我试图设置一个本地服务器,以便使用不同的库上载文件。
快递服务生:
var express = require('express');
var app = express();
var fs = require('fs');
var bb = require('express-busboy');
var app = express();
bb.extend(app, {
    upload: true,
    path: './img/',
    allowedPath: /./
});

app.post('/upload', function(req, res) {
    console.log(req.files);
});

var server = app.listen(process.env.PORT, function() {
    console.log('Listening on port %d', server.address().port);
});

令人敬畏:
var express = require('express');
var app = express();
var path = require('path');
var fs = require('fs');
var formidable = require('formidable');

app.post('/upload', function(req, res){

    // create an incoming form object
    var form = new formidable.IncomingForm();

    // specify that we want to allow the user to upload multiple files in a single request
    form.multiples = true;

    // store all uploads in the /uploads directory
    form.uploadDir = path.join(__dirname, '/img');

    // every time a file has been uploaded successfully,
    // rename it to it's orignal name
    form.on('file', function(field, file) {
        fs.rename(file.path, path.join(form.uploadDir, file.name));
    });

    // log any errors that occur
    form.on('error', function(err) {
        console.log('An error has occured: \n' + err);
    });

   // once all the files have been uploaded, send a response to the client
   form.on('end', function() {
      res.end('success');
   });

  // parse the incoming request containing the form data
  form.parse(req);

});

var server = app.listen(process.env.PORT, function() {
    console.log('Listening on port %d', server.address().port);
});

当我提出这样的请求时,两种方法都很好:
var request = require('request');
var fs = require('fs');
var formData = {
      filename: {
      value:  fs.createReadStream('ice.jpg'),
      options: {
        filename: 'ice.jpg',
        contentType: ' image/jpeg'
      }}
  };

request.post({url: 'https://myserver.com/upload', formData: formData});

或从命令行:
curl -i -X POST -H "Content-Type: multipart/form-data" -F "[email protected]" https://myserver.com/upload

但当我试着这么做的时候,它们都不起作用:
fs.createReadStream('ice.jpg').pipe(request.post('https://myserver.com/upload'));

我做错什么了吗?有办法让最后一条线工作吗?

最佳答案

好啊。我在回答这个问题,以防有人遇到类似的问题。
curl和request.post和form data选项都使用“content type:multipart/form data”头发送post请求。express busboy、greedge和multer等库用于分析数据。
这一行:

fs.createReadStream('ice.jpg').pipe(request.post('https://myserver.com/upload'));

发送带有“content type:image/jpeg”和“transfer encoding:chunked”标题的请求。这里不需要分析多部分表单数据。
服务器上的以下代码将在这种情况下工作:
var http = require('http');
var fs = require('fs');

var server = http.createServer(function(req, res){
    console.log(req.headers);
    var body = [];
    req
      .on('data', chunk => {
          body.push(chunk);
          console.log('Chunk ', chunk);
          })
      .on('end', () => {
          body = Buffer.concat(body);
          fs.writeFileSync('newfile.jpg', body, null);
      });
    res.write("OK\n");
    res.end();
});

关于node.js - 传送讯息串以要求张贴讯息无效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39022420/

10-11 01:17