【发布时间】:2012-07-28 12:14:34
【问题描述】:
将 nodeJs 与 express 一起使用。
正文解析器正在解析 POST 数据。现在我想将接收到的图像文件保存在名为 images 的快速应用程序文件夹中。该图像正在发布到 nodeJs 应用程序。当我查看回调函数中的 res.body 变量时,它包含图像,但我很困惑如何保存它。
我查看了 stackflow,但找不到任何答案。所以自己贴出来了。
我尝试过使用 fs 库及其 fs.writeFile() 函数。它会保存文件,但文件已全部损坏。
image1 是发布数据中的图像名称。使用 bodyparser()
var image = req.body.image1;
fs.writeFile('images/newImage.jpg', image, function(err){
if (err) throw err;
console.log('It is saved');
});
这会将文件保存在 images 目录中,但它不是图像。
我正在使用curl将数据发布到nodejs应用程序,代码是
$curl = curl_init();
$data = array('name' => 'First','file' => "image1=@".__dir__."/images/1.jpg");
curl_setopt($curl, CURLOPT_URL, "http://localhost:3000/api/");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_exec($curl);
curl_close($curl);
我正在记录这两个 console.log 函数
console.log(req.files);
console.log(req.body);
日志给了
{}
{ name: 'First',
file: 'image1=@C:\\xampp\\htdocs\\curltest/images/1.jpg' }
请帮助这让我发疯:-/
【问题讨论】:
-
@是的,当然!为您编辑了帖子。