multer保存文件,nidejs angular
我想在nodejs上保存映像,我正在通过帖子发送文件,如这样:
...
flag: File;
flaginput(event){
this.flag = event.target.files[0];
}
submit(){
this.http.post('localhost...', this.flag).subscribe( x => {
console.log(x.response)});
在nodejs中,
const multer = require('multer');
const storage = multer.diskStorage({
destination: function(req,file,cb){
cb(null, './imagenes/');
},
filename: function(req,file,cb){
cb(null, file.originalname)
}
});
const upload = multer({storage: storage });
module.exports = (app) =>{
app.post("/equipos", upload.single(), (req, res, next) => {
console.log(req.file)
base.query('SELECT * FROM names', (error, result) =>{
if(error){
res.json({mensaje: "error", datos: error});
}else{
res.json({mensaje: "equipo creado"});
}
})
})
但我不确定如何保存文件或为什么不保存文件,在控制台上,它似乎是任何东西错误,当我尝试使用console.log(req.file)时,在nodejs上,
我想将文件保存在文件夹中./imagenes上。
我还尝试将其发送为json {flag:this.flag},也尝试以FormData将其发送到uploadle(single()到upload.single('flag'),但不起作用,
请感谢您的帮助。
i want to save an image on nodejs, i am sending the file via post, whith angular like this:
...
flag: File;
flaginput(event){
this.flag = event.target.files[0];
}
submit(){
this.http.post('localhost...', this.flag).subscribe( x => {
console.log(x.response)});
and in nodejs
const multer = require('multer');
const storage = multer.diskStorage({
destination: function(req,file,cb){
cb(null, './imagenes/');
},
filename: function(req,file,cb){
cb(null, file.originalname)
}
});
const upload = multer({storage: storage });
module.exports = (app) =>{
app.post("/equipos", upload.single(), (req, res, next) => {
console.log(req.file)
base.query('SELECT * FROM names', (error, result) =>{
if(error){
res.json({mensaje: "error", datos: error});
}else{
res.json({mensaje: "equipo creado"});
}
})
})
but im not sure of how to save the file or why its is not saving, on console it dosnt appear to be any error, on nodejs when i try console.log(req.file) is undefined
i want to save the file on the folder ./imagenes.
i also try to send it as a json {flag: this.flag}, and also try to send it in a formData changing the upload.single() to upload.single('flag') but isn working
thanks for the help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Multer与FormData合作。
您的Multer Diskstorage配置似乎还可以,但是您应该使用FormData。
客户端请求
服务器端
Multer works with formData.
Your multer diskStorage config seems ok, but you should use formData.
Client side request
Server side