我有这个代码:
http.createServer(function (req, res) {
if (req.method.toLowerCase() === "post") {
res.writeHead(200);
req.on('data', function (data) {
console.log(data.toString());
});
res.end();
}
}).listen(8006);
如果我尝试使用 curl 之类的命令上传文件curl http://localhost:8006/upload -X POST -F file=@filetoupload.txt
,我将得到文件名和打印出的文件内容,在这种情况下如下:
------------------------------5d02ba973600
Content-Disposition: form-data; name="file"; filename="filetoupload.txt"
Content-Type: text/plain
<!-- start slipsum code -->
Do you see any Teletubbies in here? Do you see a slender
plastic tag clipped to my shirt with my name printed on
it? Do you see a little Asian child with a blank expression
on his face sitting outside on a mechanical helicopter that
shakes when you put quarters in it? No? Well, that's what
you see at a toy store. And you must think you're in a
toy store, because you're here shopping for an infant
named Jeb.
<!-- end slipsum code -->
------------------------------5d02ba973600--
现在的问题是,有没有什么好的方法可以捕获文件名的值和文件的内容?还是我必须使用某种正则表达式巫毒来提取上面内容的值?
我想在没有任何第三方模块(如 express)的情况下执行此操作,首先是因为我想了解它是如何工作的,其次是因为 express 的 bodyParser() 将所有文件保存到磁盘,然后像强大的其他模块必须从磁盘读取,并且我正在尝试制作将文件上传直接传输到 Rackspace CloudFiles 的东西,而无需先保存到磁盘。