node - post - 上传图片
2024-09-05 16:38:50
html代码:
<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head> <body>
<form action="http://127.0.0.1/dopost" enctype="multipart/form-data" method="POST">
姓名:<input type="text" name="name"> 性别:
<input type="radio" name="gender" value="男">男<input type="radio" name="gender" value="女">女 年龄:
<input type="number" name="age"> 图片:
<input type="file" name="img">
<input type="submit" value="提交">
</form>
</body> </html>
js代码
let http = require('http');
let formidable = require('formidable');
let util = require('util'); //创建服务器
let server = http.createServer(function (req, res) {
//如果你的访问地址是这个,并且请求类型是post
if (req.url == '/dopost' && req.method.toLowerCase() == 'post') {
let form = new formidable.IncomingForm();
//设置文件上传存放地址
form.uploadDir = './uploads';
//执行里面的回调函数的时候,表单已经全部接收完毕了。
form.parse(req, function (err, fields, files) {
if (err) throw err;
console.log('上传文件路径:',files.img.path,'上传文件名:',files.img.name);
// console.log(util.inspect({ fields: fields, files: files }));
//所有的文本域、单选框,都在fields存放;
//所有的文件域,files
res.writeHead(200, { 'content-type': 'text/plain' }); res.end('success!~~~');
});
}
}); server.listen(80, '127.0.0.1');
最新文章
- Swift 2.0 异常处理
- 黑马程序员_ Objective-c 之Foundation笔记(二)
- java第6次作业
- cocos2d-x内存管理(见解)
- 关于logcat日志
- FusionCharts或其它flash的div图层总是浮在最上层的问题
- 开启Mysql远程访问的所有方法
- SGU 197.Nice Patterns Strike Back
- EJB系列 - 会话Bean基础知识
- kylin客户端(python编写)不能按照预期的segment进行rebuild
- Java获取当前的年月
- SpringBoot 分布式session
- 安装python软件找不到setuptools的解决方案
- [Postman]响应(7)
- Image.fromarray的用法
- Linux useful commands
- bzoj千题计划303:bzoj4827: [Hnoi2017]礼物
- spring cloud:config-server中@RefreshScope的";陷阱";
- 1.Python爬虫入门一之综述
- C++几个重要关键字(包含借鉴其他博主的东西)
热门文章
- 浅谈android Socket 通信及自建ServerSocket服务端常见问题
- C/C++ 运算符 &; | 运算
- SCP 命令(自己总结)
- 用户空间缺页异常pte_handle_fault()分析--(下)--写时复制【转】
- 8.OpenStack网络组件
- ASP.NET webFrom
- HDU 3466 Proud Merchants【贪心 + 01背包】
- 牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)
- bzoj 2843: 极地旅行社
- Android Developer -- Bluetooth篇 概述