How to use the formidable.IncomingForm function in formidable

To help you get started, we’ve selected a few formidable examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github deltaepsilon / quiver-cms / lib / controllers / form.js View on Github external
flow: function (req, res, next) {
    var form = new formidable.IncomingForm(),
      userSlug = slug(req.headers['uid']),
      uploadDir = './uploads/' + userSlug;

    fs.ensureDirSync(uploadDir);

    form.uploadDir = uploadDir;
    form.keepExtensions = true;
    form.maxFieldSize = 100 * 1024;

    form.on('error', function (err) {
      LogService.error(err);
    });

    form.parse(req, function (err, fields, files) {
      //    console.log('parse', err, fields, files);
      var flowFilename = fields.flowFilename,
github doramart / DoraCMS / routes / system.js View on Github external
router.post('/upload', function(req, res, next) {

//    获取传入参数
    var params = url.parse(req.url,true);
    var fileType = params.query.type;
    var fileKey = params.query.key;
    var form = new formidable.IncomingForm(),files=[],fields=[],docs=[];
    console.log('start upload');

    //存放目录
    var updatePath = "public/upload/images/";
    var smallImgPath = "public/upload/smallimgs/";
    var newFileName = "";
    form.uploadDir = updatePath;

    form.on('field', function(field, value) {
        fields.push([field, value]);
    }).on('file', function(field, file) {
        files.push([field, file]);
        docs.push(file);

        //校验文件的合法性
        var realFileType = system.getFileMimeType(file.path);
github strues / boldr / packages / server / src / middleware / apolloUpload.js View on Github external
export function processRequest(req, { uploadDir } = {}) {
  // Ensure provided upload directory exists
  if (uploadDir) {
    fs.ensureDirSync(uploadDir);
  }

  const form = formidable.IncomingForm({
    // Defaults to the OS temp directory
    uploadDir,
  });

  // Parse the multipart form request
  return new Promise((resolve, reject) => {
    form.parse(req, (error, { operations }, files) => {
      if (error) {
        return reject(new Error(error));
      }
      debug('parsing form data');
      // Decode the GraphQL operation(s). This is an array
      // if batching is enabled.
      operations = JSON.parse(operations);
      // Check if files were uploaded
      if (Object.keys(files).length) {
github ysmood / nokit / test / basic.js View on Github external
createRandomServer(function (req, res) {
				const form = new require('formidable').IncomingForm();

				return form.parse(req, (err, fields) => res.end(fields['f.md'].length.toString()));
			}
github form-data / form-data / test / integration / test-custom-filename.js View on Github external
var server = http.createServer(function(req, res) {

  var form = new IncomingForm({uploadDir: common.dir.tmp});

  form.parse(req, function (err, fields, files) {
    assert(!err);

    assert('custom_everything' in files);
    assert.strictEqual(files['custom_everything'].name, options.filename, 'Expects custom filename');
    assert.strictEqual(files['custom_everything'].type, options.contentType, 'Expects custom content-type');

    assert('custom_filename' in files);
    assert.strictEqual(files['custom_filename'].name, options.filename, 'Expects custom filename');
    assert.strictEqual(files['custom_filename'].type, mime.lookup(knownFile), 'Expects original content-type');

    assert('unknown_with_filename' in files);
    assert.strictEqual(files['unknown_with_filename'].name, options.filename, 'Expects custom filename');
    assert.strictEqual(files['unknown_with_filename'].type, mime.lookup(options.filename), 'Expects filename-derived content-type');
github Yuki-Minakami / BookExample / chapter2 / 2.4 http / upload / upload.js View on Github external
function dealUpload(req,res){
    var form = new formidable.IncomingForm();//创建Formidable.IncomingForm对象
    form.keepExtensions = true;//保持原有的扩展名
    form.uploadDir = __dirname;//上传目录为当前目录
    form.parse(req,function(err,fields,files){
        if(err){throw err;}
        console.log(fields);//{ submit: 'submit' }
        console.log(files);
        res.writeHead(200, {"content-type": 'text/plain'});
        res.end('upload finished');
    })
}
server.listen(3000);
github doramart / DoraCMS / server / lib / controller / notify.js View on Github external
async addOneSysNotify(req, res, next) {

        const form = new formidable.IncomingForm();
        form.parse(req, async (err, fields, files) => {
            try {
                checkFormData(req, res, fields);
            } catch (err) {
                console.log(err.message, err);
                res.send(siteFunc.renderApiErr(req, res, 500, err, 'checkform'));
            }

            const announceObj = {
                title: fields.title,
                content: fields.content,
                adminSender: req.session.adminUserInfo._id,
                type: '1'
            }
            const newAnnounce = new NotifyModel(announceObj);
            try {
github cn007b / my / ed / nodejs / examples / whatever / uploadFile.js View on Github external
http.createServer(function (req, res) {
  if (req.url == '/fileupload') {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
      console.log(files);
      var oldpath = files.filetoupload.path;
      var newpath = '/tmp/' + files.filetoupload.name;
      fs.rename(oldpath, newpath, function (err) {
        if (err) throw err;
        res.write('File uploaded and moved!');
        res.end();
      });
    });
  } else {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write('<form enctype="multipart/form-data" method="post" action="fileupload">');
    res.write('<input name="filetoupload" type="file"><br>');
    res.write('<input type="submit">');
    res.write('</form>');
github Aaronontheweb / instant-fileserver / lib / server / router.js View on Github external
Router.prototype.parseFile = function(req, fn){
	var self = this;
	var form = new formidable.IncomingForm();
	self.logger.verbose('parsing request for file content...');

	form.parse(req, function(error, fields, files){
		if(error){ 
			self.logger.verbose('failed to parse file upload. Error: {0}'.format(JSON.stringify(error)));
			return fn(error);
		}

		var file = {};

		for(var prop in files) {
			if(files.hasOwnProperty(prop))
        		file = files[prop];
		}

		self.logger.verbose(
github iteming / nodejs-website-demo / routes / cms / views.js View on Github external
router.post('/kindeditor/fileupload', function (req, res, next) {
    var cacheFolder = '/img/uploads/';
    var userDirPath = 'public' + cacheFolder;
    if (!fs.existsSync(userDirPath)) fs.mkdirSync(userDirPath);
    var form = new formidable.IncomingForm(); //创建上传表单
    form.encoding = 'utf-8'; //设置编辑
    form.uploadDir = userDirPath; //设置上传目录
    form.keepExtensions = true; //保留后缀
    form.maxFieldsSize = 2 * 1024 * 1024; //文件大小
    form.type = true;
    var displayUrl;
    form.parse(req, function (err, fields, files) {
        if (err) {
            res.send(err);
            return;
        }
        var extName = extractExtName(files.imgFile);
        if (extName.length === 0) {
            res.send({status: 2, msg: "不支持此文件类型"});
            return;
        }

formidable

A node.js module for parsing form data, especially file uploads.

MIT
Latest version published 9 months ago

Package Health Score

86 / 100
Full package analysis