Zeka Posted April 30, 2016 Share Posted April 30, 2016 Hi. I have file field on a page wich contain pdf files. As i understand in almost all modern browsers pdf files will be opened in it, but i need to force files download. I can use "download" attribute, but it does not work in IE. Is there some build-in methods for that in PW. Thanks! Link to comment Share on other sites More sharing options...
horst Posted April 30, 2016 Share Posted April 30, 2016 No, there is nothing built-in for this. You need to send apropriate HTTP-headers. About which one, you may read here: http://stackoverflow.com/questions/17968837/file-opens-instead-of-downloading-in-internet-explorer-in-a-href-link. 1 Link to comment Share on other sites More sharing options...
adrian Posted April 30, 2016 Share Posted April 30, 2016 You might find wireSendFile with the forceDownload option useful: https://github.com/ryancramerdesign/ProcessWire/blob/7e8c7c6836282b6b64de81263f5aaa8112fd51ae/wire/core/Functions.php#L521 2 Link to comment Share on other sites More sharing options...
Zeka Posted April 30, 2016 Author Share Posted April 30, 2016 Hi, Horst and thank you for answer. For now i implement solution based on url segments. <?php if($input->urlSegment1 == 'download') { if($input->urlSegment2) { $names = array(); $urls = array(); foreach ($page->diagnostic_referral_repeater as $referral) { foreach ($referral->diagnostic_referral_files as $file) { array_push($names, $file->name); array_push($urls, $file->filename); } } $key = array_search($input->urlSegment2, $names); if($key !== false) { wireSendFile($urls[$key]); } else { throw new Wire404Exception(); } } } else if($input->urlSegment1) { // unknown URL segment, send a 404 throw new Wire404Exception(); } ?> <?php foreach ($page->diagnostic_referral_repeater as $referral) { ?> <div class="diagnostic-referral"> <div class="diagnostic-referral__box"> <div class="diagnostic-referral__title"> <?php echo $referral->diagnostic_referral_category->title; ?> </div> <?php echo $referral->render('diagnostic_referral_files', 'diagnostic-referral'); ?> <div class="diagnostic-referral__files"> <?php foreach ($referral->diagnostic_referral_files as $file) { ?> <div class="diagnostic-referral-file"> <a href="<?php echo $page->path.'download/'.$file->name; ?>" class="diagnostic-referral-file__link"> <div class="diagnostic-referral-file__icon"> <span class="icon-download"></span> </div> <div class="diagnostic-referral-file__title"> <?php echo $file->description; ?> </div> </a> </div> <?php } ?> </div> </div> </div> <?php } ?> Do you have any recommendation to improve it? Thanks. 1 Link to comment Share on other sites More sharing options...
dotnetic Posted February 8, 2017 Share Posted February 8, 2017 (edited) I used a much simpler and faster method utilizing the pagefile class. My link looks like this: // Sorry for smarty language. but thats what I used {if $page->files|@count > 0} {foreach $page->files as $file} <p> {if $file->description} {$file->description} {else} {$file->name} {/if} <br> <a href="download/{$file->name}" class="link-icon">Download</a> | {$file->filesizeStr} </p> {*{/if}*} {/foreach} {/if} and then in my _init.php I used the following code if ($input->urlSegment1 == 'download') { $download_options = array( // boolean: halt program execution after file send 'exit' => true, // boolean|null: whether file should force download (null=let content-type header decide) 'forceDownload' => true, // string: filename you want the download to show on the user's computer, or blank to use existing. 'downloadFilename' => '', ); session_write_close(); if ($input->urlSegment2) { $pagefile = $page->files[$input->urlSegment2]; if($pagefile){ wireSendFile($pagefile->filename, $download_options); } } } EDIT: The downside when implementing this in the _init.php is, that you can´t send a Wire404Exception, because this file gets included (prepended) and PHP doesn´t like exeptions in included files. If you paste this code into one of your templates you could add trowing a Wire404Exception. Edited February 10, 2017 by jmartsch 6 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now