0

我有这个代码:

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 的东西,而无需先保存到磁盘。

4

2 回答 2

1

看看http://www.componentix.com/blog/13/file-uploads-using-nodejs-once-again它应该让你开始文件上传

于 2012-06-09T14:09:13.287 回答
1

通读 Formidable 中的代码(Connect 和 Express 用于处理文件上传)并将其用作起点,修改您不喜欢的行为(例如保存到磁盘)。

于 2012-06-09T03:23:31.660 回答