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 的地址框可编辑
- File "/head.jsp" not found解决方法
- tomcat报错: File "/js_list.jsp" not found,该怎么解决
- 关于在struts2中怎么将.action加入到web.xml中的<welcome-file-list>上 急
- List items = upload.parseRequest(request);替空kindeditor SpringMvc
- 关于<welcome-file-list>的疑问。初学者郁闷哦
- Response.AddHeader("Content-Disposition" "attachment; filename=" + file.Name+"xls");中文显示乱码解决方法
- jsp重命名上传的文件提示文件无法保存(File can't be saved (1120).)解决思路
- welcome-file 404的有关问题
- 文件抛java.io.IOException: at com.sun.midp.io.j2me.file.Protocol.delete错误
- midlet.platformRequest("file:///.");打开本土Flash文件
- 關于new File(URI)路徑無法找到,该如何处理
- java io操作,file stream变换
- <input type="file" name="filename">该如何处理
- java.io.eoefException bad file number串口收发错误
- <input type="file" name="filename">解决方法
- 为何多了一个输出,结果就不一样了呢?(File)解决办法
- struts <html:file> 怎么让前面的框消失,先谢过了
- 简单有关问题。file 用哪个import定义
- The public type MyPanel must be defined in its own file,该如何处理
- /*Begin Snippet:file*/。刚接触java,这句在类前面,是啥什么意思呢?多谢您了
- return name.endsWith有用 return file.isDirectory没用 咋回事
- () PrintStream(File file)的疑惑
- 请问 java.io.File 中创建目录
- 资源文件(Resource File)和数据库(Database)哪个美
- 资源文件(Resource File)和数据库(Database)哪个好?该如何解决
- FCIAddFile(failed: code 一 [Failure opening file to be stored in cabinet]
- FCIAddFile(failed: code 1 [Failure opening file to be stored in cabinet],该怎么解决
- !一个很弱的有关问题:VS2010 cannot find or open the PDB file
- ora-08102:index key not found,obj#57848,file 六, block 6324(2)