将文件夹名称强制更改为请求中发送的名称

时间:2017-07-23 11:41:18

标签: node.js formidable

我想将文件上传到请求中指定的文件夹名称(在名称字段中)。

我曾尝试添加req.body.name,但未定义

这是我的代码

var mongoose = require('mongoose');
var DataBase = require('../models/resource').DataBase;
var formidable = require('formidable'),
    http = require('http'),
    util = require('util');
var mkdirp = require('mkdirp');
var fs = require('fs');
exports.upload = function (req, res) {
var uploadDir = './uploads/databses/'+req.body.name; 
 mkdirp(uploadDir, function (err) {
    if (err) return cb(err);
  var form = new formidable.IncomingForm({ 
  uploadDir: uploadDir,
  keepExtensions: true
});
form.on('progress', function(bytesReceived, bytesExpected) {
    console.log(100 * bytesReceived / bytesExpected + '%');
});
form.parse(req, function(err, fields, files) {      
        res.writeHead(200, {
            'content-type': 'text/plain'
        });
        res.write('received upload:\n\n');
        res.end(util.inspect({
            fields: fields,
            files: files
        }));
    });

});
};

请帮助。

1 个答案:

答案 0 :(得分:0)

解决方案我发现我将创建的文件夹移动到新文件夹

function minusTen(num) {
  var arr = '';
  for (var i = num; i > 0; i--) {
    arr += '*';
  }

  var newArr = arr.split('');

  function fn() {
    if (newArr.length)
      setTimeout(function() {
        console.log(newArr.join(" "));
        newArr.pop();
        fn()
      }, 1000)
    else 
      console.log("done, newArr.length:", newArr.length);
  }

  fn()
}

minusTen(10);