0
votes

Here's what I want to do: If a user clicks on a link (or image), a file is dynamically created and the user may download the file.

One idea is to create a link l() that calls a menu item that processes the file. However, that method will create a new page - which is not what I want. I want it to appear to the user that they are just downloading a file from a list of files (they could then go to the next file link if they would like)

This question has been asked on the drupal forum with no answer: http://drupal.org/node/427866

I assume this is an easy proposition, but no answer yet.

Thanks!

1

1 Answers

0
votes

I don't understand about the HTML's header thing, but the basic idea here is to change the header, and follows a file for the user to download.

The link (anything you want, can linked to a function, or using l()) would lead to the execution of a certain function; within that function, it would call the php function header() to return proper header to the browser before drupal does anything, then the file you want the user to download; afterwards, you call die() function to finish execution.

To the browser, it would start to download a file. But for the things you need to put in the header() function, I'm not quite sure also, you may further search on this.

Update: here's the code I've used for providing an excel-readable file for the user to download:

function download_as_excel($header, $rows, $filename = 'result.xls',$pre_content = "",$post_content = "")
{
    //Export the table to MS Excel.
    header('Content-type: application/vnd.ms-excel; charset=UTF-8');
    header('Content-Disposition: attachment; filename="'.$filename.'"');
    $output = '<html>';
    $output .= '<head><meta http-equiv=Content-Type content="text/html; charset=utf-8"></head>';
    $output .= '<body>';
  $output .= $pre_content;
    $output .= theme('table', array('header' => $header, 'rows' => $rows));
  $output .= $post_content;
    $output .= "</body></html>";
    print $output;
    exit;
}