-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload-single.js
41 lines (34 loc) · 1.23 KB
/
upload-single.js
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
31
32
33
34
35
36
37
38
39
40
41
var express = require('express');
var fs=require('fs'); //文件操作
var app=express(); //创建web应用程序
var bodyParser = require('body-parser');
app.use(express.static('public'));
app.use(bodyParser.urlencoded({extend: false}));
var multer = require('multer');
app.use(multer({dest: './upload-dir/'}).array('images'));
app.get('/upload.html',function (req,res) {
res.sendfile(__dirname+'/upload.html');
});
app.post('/upload', function(req,res){
console.log(req.files[0]);//上传的文件信息
var des_file = __dirname + '/upload-dir/' + req.files[0].originalname;
fs.readFile(req.files[0].path,function(error,data){
fs.writeFile(des_file, data, function(error){
if(error) {
console.log(error);
}else{
response = {
message: "file upload successfully",
filename: req.files[0].originalname
};
}
console.log(response);
res.end(JSON.stringify(response));
});
});
})
var server = app.listen(4000, function(){
var host = server.address().address;
var port = server.address().port;
console.log("应用实例,访问地址为:http://%s:%s",host,port);
})