0

I have written a custom module in which i have a download button like given below.

$form['topics_info_graph']['download_graph'] = array(
 '#type'  => 'button',
 '#value' =>  t('Download Graph'),
 '#button_type' => 'button',
 '#ajax' => array(
   'callback' => 'digitaliq_bckgrd_variable_ajax_download_callback', 
   'wrapper' => 'download_graph',
   'event' => 'click',
 ),
);

Onclick of this button i need to download a jpg image which is located in the temp folder. I'm able identify the file name and the path, Not sure how to download. any help?

function digitaliq_bcgrd_variable_file_download($filename) {
  $temporary_directory = variable_get('file_public_path', conf_path().'/files').'/tmp';
  $temp_path = $temporary_directory . '/';
  // Download logic should come here, any help?
}
1

2 Answers 2

0

You can set the response headers from the PHP code to trigger a browser download of the file, e.g, something like:

function digitaliq_bcgrd_variable_file_download($filename) {
  $tmp = variable_get('file_public_path', conf_path().'/files').'/tmp';
  $filepath = $tmp . '/'. $filename;
  if ($data = file_get_contents($filepath)) {
    $file = new stdClass();
    $file->contents = $data;
    $file->filename = $filename;
    return EXAMPLE_serve_download($file);
  }
  drupal_not_found();
}


/**
 * Serve a file for download
 * @param $file
 * An object keyed with filename and contents
 */
function EXAMPLE_serve_download($file, $type = 'application/octet-stream') {

  if (empty($file->contents)) {
    drupal_not_found();
  }

  $headers = array(
    'Content-Type'              => $type,
    'Content-Disposition'       => 'attachment; filename="' . $file->filename . '"',
    'Content-Transfer-Encoding' => 'binary',
    'Pragma'                    => 'no-cache',
    'Cache-Control'             => 'must-revalidate, post-check=0, pre-check=0',
    'Expires'                   => '0',
    'Accept-Ranges'             => 'bytes'
  );
  if (ob_get_level()) {
    ob_end_clean();
  }
  foreach ($headers as $name => $value) {
    drupal_add_http_header($name, $value);
  }

  drupal_send_headers();
  print $file->contents;
  drupal_exit();

}
4
  • 1
    Does it work for an AJAX call? Notice that the OP is using the "wrapper" property, and what returned from the AJAX callback replaces what is present in the wrapper.
    – avpaderno
    Commented Jan 3, 2013 at 7:26
  • The above solution doesnt work in ajax call, any further help? Commented Jan 3, 2013 at 9:06
  • 1
    Ah ok, perhaps you could use a direct link to the file instead of the ajax callback? e.g hook menu with filename parameter or similar. With the same handler, it should serve the file for download. Commented Jan 3, 2013 at 9:16
  • 1
    You can't do this in AJAX, the simplest solution will be for the button to be an ordinary link which points to the download callback as above. Commented Nov 26, 2018 at 13:51
0

You can connect to the server using the FTP protocol, and download the file to show it.

function example_download_image() {
  $local_file = 'tmp/download.jpg';
  $server_file = 'tmp_server/download.jpg';
  $ftp_server = "ftp.server.com";
  $conn_id = ftp_connect($ftp_server);
  $ftp_user_name = "user_name";
  $ftp_user_pass = "password";
  $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
  ftp_pasv($conn_id, true);
  if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
    // Show the file.
  }
  else {
    drupal_set_message("There was a problem\n");
  }
  ftp_close($conn_id);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.