使用nodeJS上传文件

Cho*_*o35 5 html javascript node.js angularjs

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

我找到了解决方案,但它只与Ajax有关,我不知道.有可能没有吗?

使用以下代码我收到此错误:

POST http://localhost:2000/database/sounds 413 (Payload Too Large)
Run Code Online (Sandbox Code Playgroud)

码:

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>
Run Code Online (Sandbox Code Playgroud)

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

        }
    } 
}
Run Code Online (Sandbox Code Playgroud)

的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);
});
Run Code Online (Sandbox Code Playgroud)

编辑:

错误成了

POST http://localhost:2000/database/sounds 400 (Bad Request)
Run Code Online (Sandbox Code Playgroud)

Man*_*dar 1

对于 json/urlencoded 限制,it\xe2\x80\x99s 建议在 server/config.json 中配置它们,如下所示:

\n\n
{\n\xe2\x80\x9cremoting\xe2\x80\x9d: {\n\xe2\x80\x9cjson\xe2\x80\x9d: {\xe2\x80\x9climit\xe2\x80\x9d: \xe2\x80\x9c50mb\xe2\x80\x9d},\n\xe2\x80\x9curlencoded\xe2\x80\x9d: {\xe2\x80\x9climit\xe2\x80\x9d: \xe2\x80\x9c50mb\xe2\x80\x9d, \xe2\x80\x9cextended\xe2\x80\x9d: true}\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

请注意,环回 REST api 有自己的带有 bodyParser.json/urlencoded 中间件的快速路由器。当您添加全局中间件时,它必须位于 boot() 调用之前。

\n\n
var loopback = require('loopback');\nvar boot = require('loopback-boot');\n\nvar app = module.exports = loopback();\n\n//request limit 1gb\napp.use(loopback.bodyParser.json({limit: 524288000}));\napp.use(loopback.bodyParser.urlencoded({limit: 524288000, extended: true}));\n
Run Code Online (Sandbox Code Playgroud)\n