简体   繁体   中英

Sending multiple images in one response object - node

I need to send multiple images with each image description via rest API. I tried something like:

files - files to be sent (binary) photos - array with photos descriptions

files.forEach(function(currentPhoto, i){
                res.write({
                photo : currentPhoto,
                description : photos[i]
            });
res.end();

This solution is not working - files are concat in one array (I want each photo (binary) at each array idx ( array[0] - first photo, array[1] - second photo ))

I want to achieve something like uploading files using multer module - I can upload multiple files with informations about current picture. Now I want to send files via rest API to client with informations about each file in one response. How can i achieve that ?

I believe you would like to create an array of all your photos and their descriptions then send it to the client.

    var photoArray = new Array();
    files.forEach(function(currentPhoto, i){
                  photoArray.push({
                     photo : currentPhoto,
                     description : photos[i]
          });
   res.render("page.ejs", {photoArray:photoArray});

This will send the photoArray you just created to an ejs file called page. On the page.ejs file you'll be able to access the photoArray using the name 'photoArray'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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