所以我刚安装了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
收到文件?实际上,我不需要明确......一个普通的节点解决方案就足够了。
答案 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