multer保存文件,nidejs angular

发布于 2025-01-26 23:41:45 字数 1115 浏览 1 评论 0原文

我想在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

病毒体 2025-02-02 23:41:45

Multer与FormData合作。

您的Multer Diskstorage配置似乎还可以,但是您应该使用FormData。


客户端请求

upload(img: File) {
  const formData: FormData = new FormData();
  formData.append("img", img);
  return this.httpClient.post('http://localhost:3000/equipos', formData);
}

服务器端

app.post('/equipos', upload.single('img'), async (req, res) => {
    return res.status(200)
        .json('Image saved')
})

Multer works with formData.

Your multer diskStorage config seems ok, but you should use formData.


Client side request

upload(img: File) {
  const formData: FormData = new FormData();
  formData.append("img", img);
  return this.httpClient.post('http://localhost:3000/equipos', formData);
}

Server side

app.post('/equipos', upload.single('img'), async (req, res) => {
    return res.status(200)
        .json('Image saved')
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文