일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- Spring
- 선택적조인
- 가우스
- sencha touch
- dock
- PLSQL
- ibsheet
- Struts
- swingx
- appspresso
- MFC
- PHP
- WebLogic
- MySQL
- Android
- jsr 296
- oracle
- 전자정부프레임워크
- rowspan
- tomcat
- jQuery
- Ajax
- phonegap
- node.js
- JSON
- iBATIS
- JDOM
- GPS
- Eclipse
- Google Map
Archives
- Today
- Total
Where The Streets Have No Name
express를 이용한 파일업로드 본문
var express = require('express')
, format = require('util').format;
var app = module.exports = express()
var fs = require('fs');
// bodyParser in connect 2.x uses node-formidable to parse
// the multipart form data.
// 업로드파일의 임시저장위치 지정
app.use(express.bodyParser({uploadDir:'D:/temp'}));
app.get('/', function(req, res){
res.send('<form method="post" enctype="multipart/form-data">'
+ '<p>Title: <input type="text" name="title" /></p>'
+ '<p>Image: <input type="file" name="image" /></p>'
+ '<p><input type="submit" value="Upload" /></p>'
+ '</form>');
});
app.post('/', function(req, res, next){
//console.log(req.files);
var tmp_path = req.files.image.path;
// set where the file should actually exists - in this case it is in the "images" directory
var target_path = './upload/' + req.files.image.name;
// move the file from the temporary location to the intended location
fs.rename(tmp_path, target_path, function(err) {
if (err) throw err;
// delete the temporary file, so that the explicitly set temporary upload dir does not get filled with unwanted files
fs.unlink(tmp_path, function() {
if (err) throw err;
res.send('File uploaded to: ' + target_path + ' - ' + req.files.image.size + ' bytes');
});
});
});
if (!module.parent) {
app.listen(3000);
console.log('Express started on port 3000');
}
참조:
http://www.hacksparrow.com/handle-file-uploads-in-express-node-js.html