2

If the upload file is an image in a Webform's result, clicking the link will show the image in the browser.

Is it possible to force to popup a Save as... dialog? Such the user downloads the file instead of viewing it?

1

This is technically not a drupal specific issue it is just how browsers deal with certain file types on the web in general.

Also note that you can right click a link to save the file always, so if this is an admin only feature then maybe that is sufficient?

If you are using Drupal's public file system for the images then it is possible to change your .htaccess file to change the behaviour of certain file types.

For more info on that see: Force files to download instead of showing up in the browser.

The .htaccess method should be the best performing and most reliable method, however if there is any reason you don't want to do it the .htaccess way there are a couple of drupal module that can help.

The File Force and Download File modules will allow you to configure file downloading however if you are making custom links to your files (for example if you are entering links directly into a text field or in custom code) you have to use a different url for the images to make sure that the links use the module's functionality, so be sure to read the documentation for whichever module you are using.

The modules are quite similar so use whichever you prefer.

[Edit] I just remembered you are interested in webform related links. Unfortunately since the current version of webform doesn't use Drupal's normal fields it won't work properly with these file downloading modules out of the box. A feature request for the modules will be needed to add webform support.

Alternatively you could modify your webform links to use the special formatting the download module you are using supports.

For example, overriding the themeing of the webform file display like this would work if you are using the file force module:

/**
 * Format the output of text data for this component
 */
function THEMENAME_webform_display_file($variables) {
  $element = $variables['element'];

  $file = $element['#value'];
  $url = (!empty($file) && !empty($file->uri)) ? webform_file_url($file->uri) : t('no upload');
  return (!empty($file) && !empty($file->uri)) ? ($element['#format'] == 'text' ? $url : l($file->filename, $url, array('query' => array('download' => '1')))) : ' ';
}

It would be a similar process with the download file module.

  • Does the .htaccess way work for the current folder and also sub-folders? Or does it work for current folder only? – ohho Feb 24 '14 at 10:43
  • 1
    Yes, .htaccess files will affect sub-directories. – rooby Feb 24 '14 at 11:07
1

You can do it overriding below webform theme function in your theme

/**
 * Format the output of text data for this component
 */
function theme_webform_display_file($variables) {
  $element = $variables['element'];
  $file = $element['#value'];
  $url = !empty($file) ? webform_file_url($file->uri) : t('no upload');
  return !empty($file) ? ($element['#format'] == 'text' ? $url : l($file->filename, $url)) : ' ';
}

Override theme function in your theme folder ... I did for seven theme using code below , replace seven with your theme name in your template.php file..

function seven_webform_display_file($variables) {
  $element = $variables['element'];
  $file = $element['#value'];
  $url = !empty($file) ? webform_file_url($file->uri) : t('no upload');
  return !empty($file) ? ($element['#format'] == 'text' ? $url : l($file->filename, $url, array('attributes' => array('download' => $file->filename)))) : ' ';
}

Trick here is to use Download attribute in HTML and it worked :-)

PS: The download attribute is not supported in Internet Explorer, Safari or Opera 12 and earlier versions.

  • 1
    Note that the download attribute doesn't work in all browsers, although that might be fine depending on your needs. – rooby Feb 24 '14 at 6:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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