forked from alibaba-fusion/next
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload-server.js
More file actions
105 lines (94 loc) · 3.75 KB
/
upload-server.js
File metadata and controls
105 lines (94 loc) · 3.75 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* test server for Upload demo
* $ node script/upload-server.js
*/
const formidable = require('formidable');
const path = require('path');
const fs = require('fs');
const http = require('http');
const port = 6001;
const tmpdir = path.resolve(__dirname, '../node_modules');
http.createServer(function(req, res) {
const method = req.method.toLowerCase();
/* eslint-disable no-console */
console.log(method, req.url);
// console.log(req.headers)
if (req.url.match('/upload.do')) {
res.setHeader('Access-Control-Allow-Origin', req.headers.origin);
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
res.setHeader('Access-Control-Allow-Credentials', 'true');
if (method === 'post') {
// parse a file upload
const form = new formidable.IncomingForm();
form.encoding = 'utf-8';
form.uploadDir = tmpdir;
form.keepExtensions = true;
form.parse(req, function(err, fields, files) {
if (err) {
res.end({ success: false, message: err });
return;
}
console.log('fields:', fields);
console.log('files:', files);
res.writeHead(200);
if (files.file) {
console.log(`recive file: ${files.file.path}`);
// res.writeHead(200, {'content-type': 'application/json'});
// res.write(`<script>document.domain='taobao.com'</script>`);
// 模拟上传时间
setTimeout(() => {
res.end(
JSON.stringify({
success: true,
message: 'success',
url: `http://${req.headers.host}${files.file.path.replace(tmpdir, '')}`,
})
);
}, Math.random() * 3000);
} else {
res.end(
JSON.stringify({
success: false,
message: 'use like name = file',
})
);
}
});
return;
} else {
res.writeHead(200, { 'content-type': 'text/html' });
res.end(`<form action="/upload.do" enctype="multipart/form-data" method="post">
<input type="file" name="file" multiple="multiple">
<br>
<input type="submit" value="submit">
</form>`);
}
} else if (req.url === '/crossdomain.xml') {
res.writeHead(200, { 'content-type': 'application/xml' });
res.end(`
<?xml version="1.0"?>
<cross-domain-policy>
<allow-access-from domain="*" />
</cross-domain-policy>`);
return;
} else if (method === 'get') {
const pathname = req.url.split('?')[0];
const realPath = tmpdir + pathname; //所有文件都存在与resources目录下
console.log(realPath);
fs.exists(realPath, function(exists) {
//判断文件是否存在
if (!exists || !fs.statSync(realPath).isFile()) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.write('Not Found!\n');
res.end();
} else {
fs.readFile(realPath, 'binary', function(err, file) {
res.writeHead(200, { 'Content-Type': 'image/jpg' });
res.write(file, 'binary');
res.end();
});
}
});
}
}).listen(port);
console.log(`Listen to: http://127.0.0.1:${port}/upload.do`);