admin管理员组

文章数量:1296332

I am sendind file-details from angular to my app.js as

onFileSelected(event: EventEmitter<File[]>) {
    const file: File = event[0];
    console.log(file); 
    const formData = new FormData();
    formData.append('file', file);
    const r = new XMLHttpRequest();
    r.open('POST', '/user/upload');
    r.send(formData);
}

then in app.js

const multer = require('multer');
const upload = multer({dest:'./pics/'});

router.post('/upload', upload.single('image'),  (req,res) => {
  const body = req.file;
  console.log(body);

  const base64Data = new Buffer(JSON.stringify(body)).toString("base64");

  console.log(base64Data);
}

and my console.log(body) gives

{ fieldname: 'image',
  originalname: '21329726_1866651723650020_188839340_o.jpg',
  encoding: '7bit',
  mimetype: 'image/jpeg',
  destination: './pics/',
  filename: '5146c9818ff517c426e34ad84ff3513f',
  path: 'pics/5146c9818ff517c426e34ad84ff3513f',
  size: 94093 
}

Now the problem is here -

1 - I don't want to upload my image/pdf file in any folder, But it is uploading in './pics/'.

2- I want to upload that file in cloudinary thats why I want to generate base64 of that file but when I am generating base64 and and uploading that in cloud, it gives an error.

I think that it is not the correct method for encoding in base64 OR I am encoding wrong data format.

Please help me.

I am sendind file-details from angular to my app.js as

onFileSelected(event: EventEmitter<File[]>) {
    const file: File = event[0];
    console.log(file); 
    const formData = new FormData();
    formData.append('file', file);
    const r = new XMLHttpRequest();
    r.open('POST', '/user/upload');
    r.send(formData);
}

then in app.js

const multer = require('multer');
const upload = multer({dest:'./pics/'});

router.post('/upload', upload.single('image'),  (req,res) => {
  const body = req.file;
  console.log(body);

  const base64Data = new Buffer(JSON.stringify(body)).toString("base64");

  console.log(base64Data);
}

and my console.log(body) gives

{ fieldname: 'image',
  originalname: '21329726_1866651723650020_188839340_o.jpg',
  encoding: '7bit',
  mimetype: 'image/jpeg',
  destination: './pics/',
  filename: '5146c9818ff517c426e34ad84ff3513f',
  path: 'pics/5146c9818ff517c426e34ad84ff3513f',
  size: 94093 
}

Now the problem is here -

1 - I don't want to upload my image/pdf file in any folder, But it is uploading in './pics/'.

2- I want to upload that file in cloudinary thats why I want to generate base64 of that file but when I am generating base64 and and uploading that in cloud, it gives an error.

I think that it is not the correct method for encoding in base64 OR I am encoding wrong data format.

Please help me.

Share Improve this question asked Aug 11, 2018 at 10:36 RupeshRupesh 8902 gold badges15 silver badges31 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

By providing the options object (in this case {dest:'./pics/'}), you're telling multer that you want to store the files in that directory. Instead, configure it to hold the files in memory:

var storage = multer.memoryStorage()
var upload = multer({ storage: storage })

According to the docs, the file object should also include a buffer property, which contains the file data. You should be able to do:

console.log(body.buffer.toString("base64"));

I would suggest you to use multiparty. A simple and easy solution handle form-data. Here you can upload files without saving it. I'm using this for save files in AWS S3

router.post('/upload',(req,res)=>{
 let form = new multiparty.Form();
    form.parse(req, function(err, fields, files) {
       //here files is array
       const base64Data = new Buffer(JSON.stringify(body)).toString("base64");
       console.log(base64Data);

    });

});

Or else you simply omit the dest object. The file will be stored in memory instead of a physical location:

本文标签: