Hi everybody!

I have a View that shows a table from "Documents" content type, where you have a FileField for document uploading, I need now a checkbox(es) in the first row, or above, that will allow the user to select certian(or all) documents and click on the button "Download all". Is it possible to do something like that with Filefield and Views, or I need some extra module for that?

Thanks in advance

Ivana

Comments

landing’s picture

Subscribing

quicksketch’s picture

It's not possible through Views or FileField directly. You'd need a module like Views Bulk Operations to create the checkboxes and the submit button, and then you'd need some custom code to provide the action to zip up and download all the files together. It's possible through this combination of modules, but I've never seen it done.

vojvotkinja’s picture

In case someone still need this, here is my solution, it's not pretty, but it works.
I had to do some custom scripting. I created this js function in my theme script.js

function downloadSelected()
{
	var documents=Array();
	var v=0;
	$(".view-Document-list form").each(function () {
		if ($(this).find(".form-checkbox").attr('checked'))
		{
			var val = $(this).find("input[name='document_url']").val();
			documents[v] = val;
		}
		v++;
	});
	$.ajax({
		url: "/sites/all/themes/apetrovic/ajax-request.php?documents="+documents,
		success: function (data)
		{
			window.location = data;
		}
	});
}

.view-Document-list you have to replace with you own class (depending on view name), and "sites/all/themes/apetrovic/ajax-request.php" is a path to custom script where you process selected files to zip and force download:

//function to zip and force download the files using PHP
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
	$zip = new ZipArchive();
	//create the file and throw the error if unsuccessful
	if ($zip->open($file_path."sites/default/files/".$archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
    	exit("cannot open <$archive_file_name>\n");
	}
	//add each files of $file_name array to archive
	foreach($file_names as $files)
	{
  		if(!empty($files))
		{			
			$file=str_replace("sites/default/files/","",$files);
			
			$zip->addFile($file_path.$files,$file);
		}
	}
	$zip->close();
	echo "sites/default/files/".$archive_file_name;
}



if($_GET['documents'])
{
	$documents=explode(",",$_GET['documents']);
	
	$file_names=$documents;
	$archive_file_name='zipped-'.time().'.zip';
	$file_path=$_SERVER['DOCUMENT_ROOT'].'/';
	zipFilesAndDownload($file_names,$archive_file_name,$file_path);
}

So now all you have to do is to add a link, that will call js function. I put it in my view footer like this:

<a href="javascript:downloadSelected()">Download selected</a>

Cheers

landing’s picture

I couldn't get this to work...are you creating the form for a custom module or are you using VBO to create the form? I tried to create a form with VBO, but can't get it to work at all.

quicksketch’s picture

Status: Active » Closed (fixed)

Closing after lack of activity.

Lan’s picture

Issue summary: View changes

Hi,
Thanks for the code. I have used it for my site and it works great but there is a problem:when i download a file that doesn't exist , it goes to "404 Page not found" page. How i can make it to say"this file doesn't exist "or have some custom word.

Thanks for your help in advance.
Lan