简体   繁体   English

在nodejs中下载图像文件后,获取消息文件不受支持。 我正在使用Formdata从angular7上传文件

[英]getting message file not supported After downloading image file in nodejs . I'm uploading file from angular7 using Formdata

I'm sending the image using formdata in angular to my nodejs api. 我正在使用formdata将图像以一定角度发送到我的nodejs api。 and in nodejs i'm storing that file at myfolder but when i open my stored image file its shows "Abc.jpeg It appears that we don't support this file format" 在nodejs中,我将该文件存储在myfolder中,但是当我打开存储的图像文件时,其显示为"Abc.jpeg It appears that we don't support this file format"

From nodejs i used multiparty, then i used formidable but getting same error in both I compared the size of file before uploading (original file) size was 78kb but after uploading the file size become 111kb. 在nodejs中,我使用了multiparty,然后使用了强大的功能,但是在这两者中都遇到了相同的错误,我比较了上传(原始文件)之前的文件大小为78kb,但是上传后为111kb。

Nodejs Code Nodejs代码

var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
    console.log(files.fileDetails.path);
    var oldpath = files.fileDetails.path;
    var newpath = 'C:/storage/myfolder/' + files.fileDetails.name;
    fs.rename(oldpath, newpath, function (err) {
        if (err) throw err;
        res.write('File uploaded and moved!');
        res.end();
    });
})

Angular Code 角度代码

public OnSubmit(formValue: any) {
    let main_form: FormData = new FormData();
    for(let j=0;j<this.totalfiles.length; j++) {
        console.log("the values is ",<File>this.totalfiles[j]);
        console.log("the name is ",this.totalFileName[j]);
        main_form.append(fileDetails,this.totalfiles[j])
    }
    console.log(formValue.items)

    this._SocietyService.postFiles(main_form).subscribe(data => {
        console.log("result is ", data)
    })
}
var path = require('path')
var multer = require('multer')

var storage = multer.diskStorage({
  destination: 'C:/storage/myfolder/',
  filename: function (req, file, cb) {
    cb(null, file.fieldname + '-' + Date.now());
  }
})

var upload = multer({ storage: storage })

You can use the multer its a vary handy middleware to handle form/multipart data. 您可以使用multer它的一个变化得心应手中间件来处理form/multipart数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM