multer无法识别带有PUT的文件

时间:2015-11-28 20:17:20

标签: node.js express file-upload put multer

所以我刚安装了node并用express启动了一个小的HTTP服务器。我正在尝试使用multer和HTTP PUT上传文件,但似乎multer无法处理PUTting文件。

使用POST做同样的事情就可以了。

这是我的main.js

var express = require('express');
var multer = require('multer');
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, './uploads')
    },
    filename: function (req, file, cb) {
        cb(null, file.originalname)
    }
});
var upload = multer({ storage: storage });
var app = express();

app.get('/', function (req, res) {
  res.send('Up and running');
});

app.put('/up/:file', upload.any(), function (req, res, next) {
  console.log('files: ' + req.files);
  console.log(req.params.file);
  res.send('got that');
})

app.post('/up', upload.any(), function (req, res, next) {
  res.send('got that');
})

var server = app.listen(8888, function(){
    var host = server.address().address;
    var port = server.address().port;
    console.log('Example app listening at http://%s:%s', host, port);
});

使用curl我可以POST文件就好了,e。 G。与curl --form "file=@someFile.jpg" http://localhost:8888/up

我尝试使用curl http://localhost:8888/up/someFile.jpg --upload-file someFile.jpg以及curl -i -T someFile.jpg http://localhost:8888/up/someFile.jpg放置文件,但它不起作用。 req.files始终为undefined

问题/ TL; DR: multer是否无法通过PUT接受文件?如何通过快递按PUT收到文件?实际上,我不需要明确......一个普通的节点解决方案就足够了。

1 个答案:

答案 0 :(得分:1)

您发送PUT请求的两种<div ng:app="searchProfile"><div ng-controller="ExampleController"> <input id="searchFor" type="text" class="form-control" ng-model="search" placeholder="Search" /> <br /> <div class="row"> <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4"> <select id="numberEvents" class="form-control" onchange="updateSearch(this.value)"> <option value="">What is the average number of delegates that attend each year</option> <option value="10-50">10-50</option> <option value="51-150">51-150</option> <option value="151-300">151-300</option> <option value="301-500">301-500</option> <option value="501-800">501-800</option> <option value="1000+">1000+</option> </select> </div> <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4"> <select id="delegates" class="form-control" onchange="updateSearch(this.value)"> <option value="">How many events do you organise each year</option> <option value="1-4">1-4</option> <option value="5-10">5-10</option> <option value="11-15">11-15</option> <option value="16-20">16-20</option> <option value="20+">20+</option> </select> </div> <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4"> <select id="delegates" class="form-control" onchange="updateSearch(this.value)"> <option value="">What type of events</option> <option value="Small meeting">Small meeting</option> <option value="Conference">Conference</option> <option value="Association">Association</option> <option value="Corporate event">Corporate event</option> <option value="Incentive and reward groups">Incentive and reward groups</option> <option value="Team building">Team building</option> <option value="Exhibitions">Exhibitions</option> <option value="Large trade shows and events">Large trade shows and events</option> <option value="Other">Other</option> </select> </div> </div> <br /> <table id="searchTextResults" class="table table-bordered"> <tr class="success"> <th>Company Name</th> <th>Events</th> <th>Delegates</th> <th>Type of Events</th> </tr> <tr ng-repeat="i in profiles | filter:search"> <td><span>{{i.Company_Name__c}}</span> </td> <td>{{i.Events_per_year__c}}</td> <td>{{i.Delegates_Each_Year__c}}</td> <td>{{i.Type_of_Events__c}}</td> </tr> </table> </div> </div> 形式不是发送curl正文,而是将原始文件内容作为正文发送。 multipart/form-data不支持,因为它期望multer - 编码形式。

要支持这些原始文件上传,您需要自己处理它们(例如将请求传送到磁盘或任何地方)。例如:

multipart/form-data