I've created a multi-step form using ctools wizard. One of my forms has a button that forces a file download using drupal's file_transfer method. This works fine until I put my form wizard into a ctools modal popup. Now when I press the "download" button on my form, I get an error message. Is there any way to force a file download from within a modal popup?

Here's my download button submit callback:

<?php
function custom_wizard_download_button_submit($form, &$form_state) {
  
    // Start the download
    $content_disposition = 'attachment; filename="' . basename($form_state['download_object']->download_uri) . '"';
    $headers = array
    (
    	'Content-type' => 'application/octet-stream',
    	'Content-Disposition' => $content_disposition,
        'Content-Transfer-Encoding' => 'binary',
    );    
    file_transfer($form_state['download_object']->download_uri, $headers);
}
?>

Here's part of the error message:

An AJAX HTTP error occurred.
HTTP Result Code: 200
Debugging information follows.
Path: /custom-download-counter/ajax/download/123/start-download
StatusText: OK
ResponseText: long wall of gibberish characters

Comments

JordanMagnuson’s picture

Issue summary: View changes

Updated issue summary.

JordanMagnuson’s picture

Issue summary: View changes

Updated issue summary.

JordanMagnuson’s picture

Issue summary: View changes

Updated issue summary.

JordanMagnuson’s picture

Title: How to force file download from ctools modal form? » Possible to force file download from ctools modal form?
Ashlar’s picture

Status: Active » Closed (won't fix)

This support request has not been active for over two months. In an effort to clean-up the issue queue this item has been closed. If all modules are up-dated and the request is still a concern please feel free to change the Status back to active.

tara@neier.org’s picture

Status: Closed (won't fix) » Active

I'm having this same problem. Is there documentation on how to work around this?

cpkeller’s picture

same problem here. Any examples or techniques to get file downloads working with ctools modals?

merlinofchaos’s picture

Status: Active » Fixed

The problem is that whatever technique you're using is loading the form via AJAX. I assume you're using it in a modal?

The technique you're attempting to use to download the file assumes the user visited the page directly, not as an AJAX call. That won't work that way.

What you'll probably need to do is to have that submit actually create an ajax redirect command which will redirect the user to a URL with the proper information to download the file. Another possibility would be to issue a command to open up a new window and direct that window to the file download page. Either way, attempting to download the file directly in the submit handler will not work via AJAX.

Since the modal assumes all buttons on the form will submit via ajax, it may or may not be possible to override click handling for that button. If it is possible, you would write a little bit of javascript to submit the form not via AJAX rather than via AJAX, and that would work. Beware that this will totally break if the form fails to validate, though, so it is probably not a good option. The redirect option is better.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

small update

grisendo’s picture

Issue summary: View changes

Since the modal assumes all buttons on the form will submit via ajax, it may or may not be possible to override click handling for that button.

If the only purpose of the form is to download that file, you can add a class 'ctools-use-modal-processed' to the form. This way, you will cheat the JS, which will think it already applied the behavior to that form, and won't apply. You can add this class in the function where you build the form if it's yours, or with a hook_form_alter if it isn't yours.

$form['#attributes']['class'][] = 'ctools-use-modal-processed';