Download file with nodeJS

I'm having trouble downloading a file with nodeJS and Angular.

I found solutions, but this is only with Ajax, which I do not know about. Is it possible to do without?

With the following code, I get this error:

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

code:

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 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); }); 

EDIT:

The error has become

 POST http://localhost:2000/database/sounds 400 (Bad Request) 
+5
source share
4 answers

If you use bodyParser

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

This will allow you to upload files up to 100mb

+2
source

To limit json / urlencoded, it is recommended that you configure them on the /config.json server as follows:

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

Please note that the loopback REST api has its own express router with bodyParser.json / urlencoded middleware. When you add the global middleware, it should appear before the boot () call.

 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})); 
+1
source

As for verifying that the data is actually a WAV file, it is best to look at the contents of the file and determine if it looks like a WAV file or not.

The WAVE PCM sound file format contains detailed format information.

To be absolutely sure that this is the correct WAV file, and it is not broken in any way, you need to check that all the fields defined there make sense. But a quick fix might just be to check that the first four bytes of the content are the letters "RIFF". It will not protect against corrupted files or malicious content, but it is a good place to start, I think.

+1
source

I tried to change the object sent to the url parameters, as stated in the Very simple AngularJS $ http POST value to "400 (invalid request)" and "Invalid HTTP status code 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); }); 

But I get a bad request error

Why no data? The console.log before this shows that I have one file in my Data form.

Error: $ http: badreq Bad request 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"}} 
0
source

Source: https://habr.com/ru/post/1268393/


All Articles