前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >在 Node 中使用 formidable 处理文件上传

在 Node 中使用 formidable 处理文件上传

作者头像
海仔
发布2021-05-18 11:17:13
1.1K0
发布2021-05-18 11:17:13
举报
文章被收录于专栏:海仔技术驿站海仔技术驿站

在 Node 中使用 formidable 处理文件上传

具体使用方式参照官方文档:https://www.npmjs.com/package/formidable

第一:安装:

代码语言:javascript
复制
# npm install --save formidable
yarn add formidable

第二:基本使用:

代码语言:javascript
复制
var formidable = require('formidable'),
    http = require('http'),
    util = require('util');
 
http.createServer(function(req, res) {
  if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
    // parse a file upload 
    var form = new formidable.IncomingForm();
 
    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}));
    });
 
    return;
  }
 
  // show a file upload form 
  res.writeHead(200, {'content-type': 'text/html'});
  res.end(
    '<form action="/upload" enctype="multipart/form-data" method="post">'+
    '<input type="text" name="title"><br>'+
    '<input type="file" name="upload" multiple="multiple"><br>'+
    '<input type="submit" value="Upload">'+
    '</form>'
  );
}).listen(8080);
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-05-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 在 Node 中使用 formidable 处理文件上传
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档