Posted by chaloum on October 7, 2011 at 5:08am
I am building a module that sits between a link for a downloadable file and the file. The link has an argument lets call it f so it would look something line /download?f=thefiletodownload.
When a user clicks on the link they are redirected to the module the asks for their details.
When submitting the form I want the file to start downloading. ATM Is redirecting to a URL which isn't what I want as it exposes the URL
I've looked at HOOK_file_download() and file_download() but don't understand how to link it in with the form submit, how to pass the filename and folder location to the hook.
It would be much appreciated if I could get some pointers on how to do this
thanks
Comments
Use form redirect and file_download
Hi,
Use form redirect on successful form submission. For Example
$form['#redirect'] = array('download', 'file=sites/default/files/abc.pdf');
$form['#redirect'] = array('download', 'file=abc.pdf');
Check for path whether sites/default/files or directly filename
Then in the callback function of download menu path just call
file_download();
That should download the file.
For more information
http://api.drupal.org/api/drupal/includes--file.inc/function/file_download
Cheers,
Anil Sagar,
Lead Drupal Developer,
SourceN,
http://www.sourcen.com
http://www.anilsagar.com -- Drupal Tutorials, Drupal Tips, Drupal Performance Solutions
I'm not quite understanding
I'm not quite understanding your information
I assume I put $form['#redirect'] = array('download', 'file=myfilenameandpath'); ar the end of the hook_submit()
and the call the file_download() is that part of the hook_menu that called the form. The callback for that hook_menu is drupal_get_form, do I add the file_download() to the call back as an array?
or do I need to create another hook_menu to handle the file_download()?
thanks
Use hook form alter or in form implementation
Use $form['#redirect'] = array('download', 'file=myfilenameandpath'); in form implementation if custom form.
If form is defined using a content type or any other module then alter the form using hook_form_alter then place above code.
Then implement a hook_menu and define menu called download and in the page callback simply call file_download function.
Cheers,
Anil Sagar,
Lead Drupal Developer,
SourceN,
http://www.sourcen.com
http://www.anilsagar.com -- Drupal Tutorials, Drupal Tips, Drupal Performance Solutions
I ended up coding the
I ended up coding the functionality myself as the trying to get the Drupal function working wasn't going to work and the documentation is to vague and was starting to looking like an either or proposition where going straight from a form submission directly to a download couldn't be done in the one process
I have a similar problem
I'm trying to build a module that shows users who click file links a form that must be filled before file download starts. I tried to use something like
<?phpfunction MYMODULE_file_download($uri)
{
return drupal_get_form('MYFORM');
}
?>
But I guess it's wrong on soooo many levels... no wonder it is not working. Can you please share your experience - how do you treat downloadable file links?