Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

CookieMonster's avatar

unable to download zip file

I have a table with list of orders where each order have their own airway bill note. The airway note is actually an external URL(given by API response) where if I click on it, it will present a download box to user to download and save the file. I now wanted to allow the user to select multiple consignment notes so they can download in bulk.

I thought of having a checkbox for user to select and then in my controller, create a zip file and add those airway to it and return it to the user to download. Currently, I am using this zip package by zanysoft/laravel-zip. I am not too sure if it allows to add external URL inside the zip file.

Here is my Ajax request:

  $('.download_bulk').on('click', function(e) {
            e.preventDefault();
            let allVals = [];
            $(".subChkOrder:checked").each(function() {
                allVals.push($(this).attr('data-id'));
            });
            if( allVals.length <= 1 ) {
                alert("Please select more than 1 row.");
            } else {            
                    let join_selected_values = allVals.join(",");
                    $.ajax({
                        url:"{{route('order.print.bulk')}}",
                        type: 'POST',
                        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                        data: 'ids='+join_selected_values,
                        success: function (data) {
                            //window.location = data;
                            if(data['success']) {
                               alert(data['data']);
                               
                            } else if (data['error']) {
                                alert(data['error']);
                            } else {
                                alert('Whoops Something went wrong!!');
                            }
                        },
                        error: function (data) {
                            alert(data.responseText);
                        }
                    });
                
            }
        });

And in my controller:

public function printBulkConsignment(Request $request){
        $ids = $request->ids;
       
        $datas = Order::whereIn('id', explode(',', $ids))->get();
        $consignments= array();

        foreach($datas as $data){
            $consignments[] = '"'.$data->awb_id_link.'"';
        }

        $zip = Zip::create('file.zip');

        $zip->add($consignments);

        $zip->close();

        $data = json_encode($zip);
       
        return response()->download($zip);
        //return response()->json(['success' => 'Print bulk success', 'data' =>$data]);
    }

The awb_id_link is external URL like "https:..../external_url/path_to_download" Upon submitting the request, the alert keeps showing an error:

message : Object of class Zanysoft\Zip could not be converted to string

I am not too sure why the user is unable to download the zip, how do I solve it?

0 likes
13 replies
neilstee's avatar

@nickywan123 I think you need to pass the file name of the zip file, not the zip object class, like:

return response()->download('file.zip');

But make sure you are passing the correct path.

Snapey's avatar

I would do a post to your server so that it can respond with a download..... not an ajax request.

newbie360's avatar
public function printBulkConsignment(Request $request)
{
    $ids = $request->ids;
   
    $links = Order::whereIn('id', explode(',', $ids))
                ->pluck('awb_id_link')
                ->toArray();
    dd(links); // <----- add this

    $zip = Zip::create('file.zip');
    $zip->add($links);
    $zip->close();
    $data = json_encode($zip);
   
    return response()->download($zip);
    //return response()->json(['success' => 'Print bulk success', 'data' =>$data]);
}

i don't think you can create a directory like this https://example.com

CookieMonster's avatar

dd($links) gives me:

 0 => "https://connect.URL.my/?ac=AWBLabel&id=RVAtRkJXa05Fb3dQIzEzMTUzNTExMw%3D%3D"
  1 => "https://connect.URL2.my/?ac=AWBLabel&id=RVAtdEJSakU1VnB6IzIyNjQ2OTI0"
CookieMonster's avatar

Gives this error: Object of class ZanySoft\Zip\Zip could not be converted to string

CookieMonster's avatar

Means my code is fine, just that the package couldn't add URL to zip?

How would I append them into a file using the library?

newbie360's avatar

test, change

$zip->add($links);

to

$zip->add('test_folder');

any errors ?

CookieMonster's avatar

Same error.

I believe the error comes from this code: return response()->download($zip); since download takes in 1st argument as path to file.

I also am not using Ajax anymore, I switched to this:

    $('.download_bulk').on('click', function(e) {
            e.preventDefault();
            let allVals = [];
            $(".subChkOrder:checked").each(function() {
                allVals.push($(this).attr('data-id'));
            });
            if( allVals.length <= 1 ) {
                alert("Please select more than 1 row.");
            } else {            
                    let join_selected_values = allVals.join(",");
                    const newUrl = "{{route('order.print.bulk')}}?ids=" + allVals.join(",")
                    window.location = newUrl;
                
            }
        });
newbie360's avatar

so just give the path of the zip =(

but the zip file is created ? did you check what inside the zip ? or try unzip it

to add the urls, if you mean url shortcut file *.url

in Windows create a shortcut, input a url and give a name, this file is store as name.url

open it in your editor, can see something like this

[InternetShortcut]
URL=http://example.com

double click this file will open the browser and go to that url

CookieMonster's avatar

Don't think I can do it that way. Cause the URL pops out the download box.

Snapey's avatar

You want to create a zip file containing text links?

And then you want to try and send that zip file as a json response to the client?

What planet does this work ok?

Please or to participate in this conversation.