0

Im trying to use Multer to upload an array of images. At the client side i have a FormData called pictures.

Step 1 – Create formData with all images:

const data = new FormData();

pictures.forEach(pic => {
        data.append('pictures', {
          fileName: pic.fileName,
          uri: pic.image,
          type: pic.type,
        });
      });
const headers = {
    'Content-Type': 'multipart/form-data',
    'x-access-token': token,
  };

const diaryUpdatePost = await post(`diary/uploadPictures/{diary}`, body, {     headers,   }); </pre></div><!-- /wp:codemirror-blocks/code-block --> <!-- wp:paragraph -->Step 2 - Get the request at server side. Im setting up multer and routers:<!-- /wp:paragraph --> <!-- wp:codemirror-blocks/code-block {"showPanel":false,"languageLabel":"no","mode":"clike","mime":"text\/x-c++src"} --> 			<div class="wp-block-codemirror-blocks-code-block code-block"><pre>const router = express.Router(); const multer = require('multer');  const storage = multer.diskStorage({   destination(req, file, cb) {     cb(null, 'uploads/');   },   filename(req, file, cb) {     cb(null, `{file.fieldname}-{Date.now()}`);   }, });  const upload = multer({ storage, limits: { fieldSize: 25 * 1024 * 1024 } });  // Multer with the same FormData (client) router.post('/uploadPictures/:name', upload.array('pictures'), diaryController.uploadDiaryPictures); </pre></div><!-- /wp:codemirror-blocks/code-block --> <!-- wp:paragraph -->And finally my diaryController, where i need to get all files:<!-- /wp:paragraph --> <!-- wp:codemirror-blocks/code-block {"showPanel":false,"languageLabel":"no","mode":"clike","mime":"text\/x-c++src"} --> 			<div class="wp-block-codemirror-blocks-code-block code-block"><pre>exports.uploadDiaryPictures = async (req, res) => {   // Logging []. I cant access files from here   console.log(`files{req.files}...`);
};

I already tried to use express-fileupload, but req.files return undefined. Some ideia to help? Thx.

Anonymous Asked question May 14, 2021