{"id":2413,"date":"2021-06-09T14:12:28","date_gmt":"2021-06-09T19:12:28","guid":{"rendered":"https:\/\/xlinesoft.com\/blog\/?p=2413"},"modified":"2021-06-09T14:14:31","modified_gmt":"2021-06-09T19:14:31","slug":"zip-and-download-selected-files","status":"publish","type":"post","link":"https:\/\/xlinesoft.com\/blog\/2021\/06\/09\/zip-and-download-selected-files\/","title":{"rendered":"Zip and download selected files"},"content":{"rendered":"<p>This tutorial covers a simple but common need. You have a page with a list of files and want your users to select and download multiple files at once. In this example, we will use the upload field that can store multiple files at once. <\/p>\n<p><a href=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/06\/DownloadSelected.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/06\/DownloadSelected-300x234.png\" alt=\"\" width=\"300\" height=\"234\" class=\"alignnone size-medium wp-image-2415\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/06\/DownloadSelected-300x234.png 300w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/06\/DownloadSelected-768x600.png 768w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/06\/DownloadSelected.png 945w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n<!--more--><\/p>\n<p>In Page Designer proceed to the List page and add a Custom button. <\/p>\n<p><strong>ClientBefore event<\/strong><\/p>\n<p>In this event, we simply check if any records were actually selected and display a message if there is nothing to process.<\/p>\n<pre name=\"code\" class=\"javascript:nocontrols\">\r\nif ($(\"[name^=selection]:checked\").length<1) {\r\n  swal(\"Select at least one record\");\r\n  return false;\r\n}\r\n<\/pre>\n<p><strong>Server event<\/strong><\/p>\n<p>In this event, we build the list of files and zip them up. We save the zip file in the temporary folder (templates_c) and pass the name of the file to <strong>Client After<\/strong> event. If something didn't work we just pass the error message to the same event. <\/p>\n<p><strong>PHP code<\/strong><\/p>\n<pre name=\"code\" class=\"php:nocontrols\">\r\n$filesArray = array();\r\nwhile($record = $button->getNextSelectedRecord()){\r\n\t$files = my_json_decode($record[\"files\"]);\r\n\tforeach($files as $f){\r\n\t\t$filesArray[] = $f;\r\n\t}\r\n}\r\n$zip = new ZipArchive();\r\n$filename = \"templates_c\\\\DownloadSelected\".date(\"YmsHis\").\".zip\";\r\n$result[\"error\"] = \"\";\r\n$result[\"name\"] = $filename;\r\nif($zip->open(getabspath($filename), ZipArchive::CREATE)!==TRUE) {\r\n    $result[\"error\"] = \"Cannot create zip file\";\r\n}\r\n\r\nforeach($filesArray as $v)\r\n\t$zip->addFile(getabspath($v[\"name\"]),$v[\"usrName\"]);\r\n<\/pre>\n<p><strong>Client After event<\/strong><\/p>\n<p>Here we display the error message if any. If there is no error message we simply redirect the user to the zip file and it will be downloaded automatically. Then we clear all the checkboxes so the page is ready for the next step. <\/p>\n<pre name=\"code\" class=\"javascript:nocontrols\">\r\nif(result[\"error\"]!=\"\")\r\n\tajax.setMessage(result[\"error\"]);\r\nelse{\r\n\tlocation.href=result[\"name\"];\r\n\t$(\"[name^=selection]\").each(function(){\r\n\t\t$(this).prop(\"checked\",\"\");\r\n\t});\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial covers a simple but common need. You have a page with a list of files and want your users to select and download multiple files at once. In this example, we will use the upload field that can store multiple files at once.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,1,8],"tags":[],"class_list":["post-2413","post","type-post","status-publish","format-standard","hentry","category-asp-net","category-php-category","category-tutorials"],"_links":{"self":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2413","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/comments?post=2413"}],"version-history":[{"count":6,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2413\/revisions"}],"predecessor-version":[{"id":2420,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2413\/revisions\/2420"}],"wp:attachment":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/media?parent=2413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/categories?post=2413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/tags?post=2413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}