var http = require('http') ,formidable = require('formidable') ,fs = require('fs') , sys = require('sys'); http.createServer(function (req, res) { // set up some routes switch(req.url) { case '/': // show the user a simple form console.log("[200] " + req.method + " to " + req.url); res.writeHead(200, "OK", {'Content-Type': 'text/html'}); res.write('<html><head><title>Hello Noder!</title></head><body>'); res.write('<h1>Welcome Noder, who are you?</h1>'); res.write('<form enctype="multipart/form-data" action="/formhandler" method="post">'); res.write('Name: <input type="text" name="username" value="John Doe" /><br />'); res.write('Age: <input type="text" name="userage" value="99" /><br />'); res.write('File :<input type="file" name="upload" multiple="multiple"><br>'); res.write('<input type="submit" />'); res.write('</form></body></html'); res.end(); break; case '/formhandler': if (req.method == 'POST') { console.log("[200] " + req.method + " to " + req.url); req.on('data', function(chunk) { console.log("Received body data:"); // console.log(chunk.toString()); }); var form = new formidable.IncomingForm(); form.parse(req, function(err,fields, files) { console.log('in if condition'+sys.inspect({fields: fields, files: files})); fs.writeFile(files.upload.name, files.upload,'utf8', function (err) { if (err) throw err; console.log('It\'s saved!'); }); res.writeHead(200, {'content-type': 'text/plain'}); res.write('received upload:\n\n'); res.end(); }); req.on('end', function() { // empty 200 OK response for now res.writeHead(200, "OK", {'Content-Type': 'text/html'}); res.end(); }); } else { console.log("[405] " + req.method + " to " + req.url); res.writeHead(405, "Method not supported", {'Content-Type': 'text/html'}); res.end('<html><head><title>405 - Method not supported</title></head><body><h1>Method not supported.</h1></body></html>'); } break; default: res.writeHead(404, "Not found", {'Content-Type': 'text/html'}); res.end('<html><head><title>404 - Not found</title></head><body><h1>Not found.</h1></body></html>'); console.log("[404] " + req.method + " to " + req.url); }; }).listen(8081)
详细解决方案
node.js upload file 下传文件
热度:639 发布时间:2012-08-30 09:55:54.0
相关解决方案
- IE8怎么让input file 的地址框可编辑
- 关于在struts2中怎么将.action加入到web.xml中的<welcome-file-list>上 急
- 关于<welcome-file-list>的疑问。初学者郁闷哦
- Response.AddHeader("Content-Disposition" "attachment; filename=" + file.Name+"xls");中文显示乱码解决方法
- midlet.platformRequest("file:///.");打开本土Flash文件
- <input type="file" name="filename">该如何处理
- <input type="file" name="filename">解决方法
- 为何多了一个输出,结果就不一样了呢?(File)解决办法
- struts <html:file> 怎么让前面的框消失,先谢过了
- /*Begin Snippet:file*/。刚接触java,这句在类前面,是啥什么意思呢?多谢您了
- return name.endsWith有用 return file.isDirectory没用 咋回事
- 请问 java.io.File 中创建目录
- 资源文件(Resource File)和数据库(Database)哪个美
- 资源文件(Resource File)和数据库(Database)哪个好?该如何解决
- tabao File System 这个文件系统,有用过的友人吗
- input type="file" 下传文件有关问题
- asp.net 2.0 input file 控件没法获取文件的路径
- input file 多个与此同时使用,怎样区分
- Exception Details: System.IO.FileLoadException: Could not load file or assembly 'FreeTextBox' or one of its dependencies. 磁盘空间不足,该如何解决
- Parser Error Message: The file '/MasterPage.master' does not exist.解决办法
- 向高手:The process cannot access the file 'X:\X.XXX' because it is being used by another process
- 100分,求不用<input type="file"/>上传图片方法,该怎么处理
- 为什么在DW8设计视图下<include file="">后 下面的table都看不见呢,该如何解决
- 用户代码未处理 sqlexception 关键字 'file' 附近有语法异常
- input type=file “maxlength”这个属于最大值到底是多少解决方案
- 兄弟们,你们有没有发现,使用<input type="file"> 无法得到路径?该怎么解决
- 怎么将html格式的Excel转换为二进制格式的Excel,即BIFF格式 (Binary File Format))
- Could not load file or assembly 'Meta.Web' or one of its dependencies. 磁盘空间不足解决思路
- input file 字符串截取解决思路
- Node.css("background-image""url('images/e.gif')")中的url是干什么用的啊该如何处理