How to send multiple files in postman ReSTful web service?
Asked Answered
V

7

79

I am using ReSTful webservice. I am uploading multiple photos with one function (PHP).

I have used $num_files = count($_FILES['myfile']['name']) to count number of files that are to be uploaded but this always gives 1:

Image

When I print $_FILES['myfile']['name'] or $_FILES it returns last image.

Am I suppose to do any setting to send multiple files at a time?

<?php
if($result=="success")
{
    $num_files = count($_FILES['myfile']['name']);
    Zend_Debug::dump($num_files);
    die;
    for( $i=0; $i < $num_files; $i++ )
    {
        $name = $_FILES["myfile"]["name"][$i];
        $temp_path = $_FILES['myfile']['tmp_name'][$i];
        $image_name = Helper_common::getUniqueNameForFile( $name );

        echo $image_name;
        die;
        // Set the upload folder path
        $target_path = $originalDirecory."/";


        // Set upload image path
        $image_upload_path = $target_path.$image_name;
        move_uploaded_file($temp_path, $image_upload_path);

        //if(move_uploaded_file($temp_path, $image_upload_path))
        //{
        // Set 800*800 popup thumbnail...
        // Set popup directory...
        $thumbnail_directory=$popUpDirectory."/";
        // Set thumbnail name...
        $thumb_name1=$thumbnail_directory.'thumbnail_'.$image_name;
        // Set width and height of the thumbnail...
        $thumb_width=800;
        $thumb_height=800;
        $thumb1=Helper_common::generateThumbnail($image_upload_path, $thumb_name1, $thumb_width, $thumb_height);

        //if($thumb)
        //{
            // Set 435*333 thumbnail...
            // Set thumbnail directory...
            $thumbnail_directory=$wallDirecory."/";
            // Set thumbnail name...
            $thumb_name2=$thumbnail_directory.'thumbnail_'.$image_name;
            // Set width and height of the thumbnail...
            $thumb_width=435;
            $thumb_height=435;
            $thumb2=Helper_common::generateThumbnail($image_upload_path, $thumb_name2, $thumb_width, $thumb_height);

            //if($thumb)
            //{
                // Set 176*176 thumbnail...
                // Set thumbnail directory...
                $thumbnail_directory=$galleryDirectory."/";
                // Set thumbnail name...
                $thumb_name3=$thumbnail_directory.'thumbnail_'.$image_name;
                // Set width and height of the thumbnail...
                $thumb_width=176;
                $thumb_height=176;
                $thumb_smart_resize_3 = Helper_ImageResizer::smart_resize_image($image_upload_path, NULL, $thumb_width, $thumb_height, false, $thumb_name3, false);
                $thumb3=Helper_common::generateThumbnail($image_upload_path, $thumb_name3, $thumb_width, $thumb_height);
            //if($thumb)
            //{
                $profile_thumb=$thumb3;
                // Set 131*131 thumbnail...
                // Set thumbnail directory....
                $thumbnail_directory = $thumbnailsDirectory."/";
                // Set thumbnail name....
                $thumb_name4 = $thumbnail_directory.'thumbnail_'.$image_name;
                $thumb_width=131;
                $thumb_height=131;
                $thumb_smart_resize_4=Helper_ImageResizer::smart_resize_image($image_upload_path, NULL, $thumb_width, $thumb_height, false, $thumb_name4, false);
                $thumb4=Helper_common::generateThumbnail($image_upload_path, $thumb_name4, $thumb_width, $thumb_height);

                }
Vietnamese answered 28/1, 2015 at 5:31 Comment(1)
The curly brackets are incomplete can you please post the full code for the sectionAugustaugusta
V
113

I got a solution. I need to make myfile an array like this: myfile[] :)

Vietnamese answered 28/1, 2015 at 5:51 Comment(2)
any array of data you have to mention it with [ ] , in your case you have to send multiple files via array [ ] as in in postman you have to mention it in key parameter myfile[ ]Maybellemayberry
I guess it's not needed anymore or added automatically. At least it works without it in my case.Soult
P
92

You need to add a square bracket [] sign to the parameter. Look at the following image. I add file[] to upload multiple images from the postman.

enter image description here

Putput answered 11/3, 2019 at 14:45 Comment(1)
Thanks for answer, I use api platform and I thought that he add this when you set open api schema in array, but it's false. I have to add square bracket for to works.Quijano
K
37

You can simple add multiple lines with same key and postman converts them to array. No need to add [] as suffix to key.

Request

enter image description here

Response

enter image description here

If you have an array objects that need to passed then follow below pattern

enter image description here

Karns answered 18/8, 2019 at 12:15 Comment(1)
This is the correct answer. The [] is just and artifact from PHP that is not standard in all languagesKwok
C
14

Update 2020 Postman

You don't need to add [ ] just put the param name and select files.

Clue answered 24/6, 2020 at 21:22 Comment(3)
works in the 2021 versionMaure
You don't, but you'll get only one file on the backend. First one, probably.Boutonniere
mmm no, you get all of themUndernourished
S
6

I am using Postman v9.21.0

Steps:

  1. Create a new Post request in Postman. Like https://localhost:44394/api/BlobStorage/UploadFiles
  2. Click on Body and then select form-data
  3. Type the key name like "files" in the KEY column
  4. Here is the tricky part. Hover the mouse on entered key files. You will see Text on the right corner. Change it to File. Now you will see Select Files in the VALUE column.
  5. Click Select Files to select the files you want to upload.
  6. Click Send after selecting the files.

enter image description here

Sylvester answered 13/6, 2022 at 13:44 Comment(0)
M
3

Yeah,last version of postman made a bad update.

But, you can download the chrome extensions of postman and send multiple files with it, it still works there.

Edit: I just checked now, they brought it back, but you have to reenter the key value for files if you made them with the chromium version.

But now it works fine.

see the image

enter image description here

Mamey answered 8/4, 2020 at 4:21 Comment(0)
M
0

This is how you can upload through postman when working with Django Rest Framework

enter image description here

Monadism answered 6/7, 2024 at 8:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.