用nodeJS上传文件

我在使用nodeJS和Angular上传文件时遇到了问题。

我find了解决scheme,但只有Ajax我不知道。 是否有可能没有?

用下面的代码,我得到这个错误:

POST http://localhost:2000/database/sounds 413 (Payload Too Large) 

码:

HTML:

 <div class="form-group"> <label for="upload-input">This needs to be a .WAV file</label> <form enctype="multipart/form-data" action="/database/sounds" method="post"> <input type="file" class="form-control" name="uploads[]" id="upload-input" multiple="multiple"> </form> <button class="btn-primary" ng-click="uploadSound()">UPLOAD</button> </div> 

使用Javascript:

 $scope.uploadSound = function(){ var x = document.getElementById("upload-input"); if ('files' in x) { if (x.files.length == 0) { console.log("Select one or more files."); } else { var formData = new FormData(); for (var i = 0; i < x.files.length; i++) { var file = x.files[i]; if(file.type==("audio/wav")){ console.log("Importing :"); if ('name' in file) { console.log("-name: " + file.name); } if ('size' in file) { console.log("-size: " + file.size + " bytes"); } formData.append('uploads[]', file, file.name); }else{ console.log("Error with: '"+file.name+"': the type '"+file.type+"' is not supported."); } } $http.post('/database/sounds', formData).then(function(response){ console.log("Upload :"); console.log(response.data); }); } } } 

的NodeJS:

 //Upload a sound app.post('/database/sounds', function(req, res){ 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, '/database/sounds'); // 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); }); 

编辑:

错误成了

 POST http://localhost:2000/database/sounds 400 (Bad Request) 

       

网上收集的解决方案 "用nodeJS上传文件"

如果你正在使用bodyParser

 app.use(bodyParser.urlencoded({limit: '100mb',extended: true})); app.use(bodyParser.json({limit: '100mb'})); 

这将允许您上传文件高达100MB

对于json / urlencoded限制,build议在server / config.json中configuration它们如下:

 { “remoting”: { “json”: {“limit”: “50mb”}, “urlencoded”: {“limit”: “50mb”, “extended”: true} } 

请注意,loopback REST API具有自己的expressParser.json / urlencoded中间件。 在添加全局中间件时,必须在boot()调用之前。

 var loopback = require('loopback'); var boot = require('loopback-boot'); var app = module.exports = loopback(); //request limit 1gb app.use(loopback.bodyParser.json({limit: 524288000})); app.use(loopback.bodyParser.urlencoded({limit: 524288000, extended: true})); 

关于检查数据实际上是一个WAV文件,最好的办法是看文件的内容,并确定它是否看起来像一个WAV文件。

WAVE PCM声音文件格式文章进入格式的细节。

要绝对确定这是一个适当的WAV文件,并且不会以某种方式破坏,您需要检查在此定义的所有字段是否有意义。 但是一个快速的解决scheme,可能只是检查内容的前四个字节是字母“RIFF”。 它不会防范损坏的文件或恶意内容,但这是一个开始我想的好地方。

我试图改变发送到url params的对象,如非常简单AngularJS $ http POST结果'400(Bad Request)'和'无效的HTTP状态代码400'中所述 :

 $http.post({ method: 'POST', url: '/upload', data: formData, headers: {'Content-Type': 'application/x-www-form-urlencoded'}, transformRequest: function(obj) { var str = []; for(var p in obj) str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); return str.join("&"); } }).success(function(response){ console.log("Uploaded :"); console.log(response.data); }); 

但是我得到一个错误的请求错误

为什么没有收到数据? 这之前的console.log显示我的formData中有一个文件。

错误:$ http:badreq错误的请求configuration

 Http request configuration url must be a string or a $sce trusted object. Received: {"method":"POST","url":"/upload","data": {},"headers":{"Content-Type":"application/x-www-form-urlencoded"}}