Prevent link to file from downloading? Open instead?

Permalink 2 users found helpful
When editing text, you can select a piece of text, and click add link to file, but how do you prevent the file from always downloading. How can I a PDF file to open in the clients default pdf application, instead of downloading it?

 
Fitzy replied on at Permalink Reply
Fitzy
Whatever happens, PDF files have to be downloaded in order to open on the person's machine. That person has to set what he wants to happen with PDF files. For example in Firefox under Tools, there's "PDF Downloads options" where they can set whether to open the file or save it or even open as HTML.
izne replied on at Permalink Reply
that is for normal people. you and me and everybody normal is pretty comfortable with this.
but imagine a client-idiot which wants the pdf to be opened in the same window and this is his requirement.
I have this problem now and I am searching for some workaround. pdfs are forced to be downloaded and I really need just a link to them, not to the download files controller, which forces them to download. any idea would be helpful.
frz replied on at Permalink Reply
frz
its been a while, but how your browser handle's PDF's is specific to your browser, isn't it? I dont remember a embed statement for PDF's or anything like that..
Unitedpr replied on at Permalink Reply
To add to discussion about PDF's downloading instead of just opening. I ahve checked my Firefox browzer sttings and they indicate that the PDF's should open in the Firefox browzer. I have setup a hyperlink to have the PDF file open in a new (_blank) tab or window but it still insists on asking me what to do. I.E. "You are attempting to open a file" dialogue. I have other websites not using Concrete5 and do not have a problem, the files just simply open a new tab and display the PDF just fine. Based on this I am concluding it must be a problem with Concrete5.
CodeMonkey replied on at Permalink Reply
CodeMonkey
Does anyone have a resolution for this one? Is there some kind of setting I can turn on or off?
cannonf700 replied on at Permalink Reply
cannonf700
I'm having the same problem. For now I'm using 'www.scribd.com' to host my .pdf and then using the HTML block to post the code from 'scribd' this lets you display the pdf inline with a download and print link.
pthalacker replied on at Permalink Reply
I am have the same requirement. I don't see anyone from the Concrete team weighing in. Surely someone has come up with a solution for this
Mnkras replied on at Permalink Reply
Mnkras
it depends on the headers sent, c5 uses by default headers which force a download compared to the default in most browsers which is to view
waterfeller replied on at Permalink Reply
waterfeller
If you simply enter a hyperlink directly to the full URL of the PDF file, it will open in your browser it you have that setting in your browser. This will work even if you have uploaded the PDF into the c5 file manager as long as you use the real URL. If course, if you replace the file, you will have to go update the URL manually.

If you use Add File to place the link on the page, or any of the other mechanisms for listing files from the file manager, then you will be forced into download.

I believe that concrete5 should offer the option of download or open.
waterfeller replied on at Permalink Reply
waterfeller
Any chance of an answer on this?

I hate the thought of having to change links to about 50 pdf's to explicit hyperlinks and then change them back if this is ever fixed.
isha replied on at Permalink Reply
Anybody?
jgarcia replied on at Permalink Best Answer Reply
jgarcia
There's basically two options in order to allow opening rather than forcing download:

1. Change your links. By default, they will say
http://www.mysite.com/index.php/download_file/1234...
Simply add /view_inline after /download_file, resulting in a link that looks something like this:
http://www.mysite.com/index.php/download_file/view_inline/1234...

2. Option two would be to create a file to override the core "download_file" single page. Simply copy /concrete/controllers/download_file.php to /controllers/download_file.php. In the first line of the view() function, add:

$this->view_inline($fID);


The only issue this will cause is that the user will not see a file name when the download prompt comes up. You can fix this by adding the following code to the view_inline() function after the line that says "header("Content-type: $mimeType");"

$fv = $file->getApprovedVersion();
header('Content-disposition: attachment; filename="' . $fv->getFileName() . '"');
waterfeller replied on at Permalink Reply
waterfeller
This is the best answer!

The simplest thing for me is to change the links, so I have not tried the other alternative. This allows me to have some PDF's that open and others that download.

Thanks jgarcia!

P.S. It would be nice if the C5 interface would allow this choice in the "Add File" command of the content block editor...
mesuva replied on at Permalink Reply
mesuva
This solutions works great. I made one change though, I made it so that pdfs view inline in the browser (I see that as a bit more user friendly) instead of forcing the download. If you do a 'Download Linked File' or equivalent, the browser still knows the correct filename.

So I did everything in jgarcia's post, but changed the header line to:

header('Content-disposition: inline; filename="' . $fv->getFileName() . '"');


(the difference being the word inline instead of attachment)

Cheers!
mesuva replied on at Permalink Reply
mesuva
If anyone is following this, I made a further change to the view_inline function.
In IE with Acrobat Reader, when a user does a 'Save As', that combination doesn't respect the filename sent in the header... it just makes up a filename to save the file as based on the url. (grrrr... all other browsers 'see' the intended filename in the header just fine).

So I read that a solution is to make sure the url has a real filename on it as well as a variable called file, so instead of:

index.php/download_file/view/94/1/

the url should be
/index.php/download_file/view/94/1/Car_Boot_Sale_Poster_2011.pdf?file=Car_Boot_Sale_Poster_2011.pdf

I looked at changing the way that C5 places the links in the content editor side of things, but that looked really fiddly, so I did a little bit of a hack to redirect the browser instead. It's not optimal because it ends up doing two page loads instead of one, but it does work well.

The change is the if conditioned redirect and a reshuffling of some of the other lines.

public function view_inline($fID) {
      $file = File::getByID($fID);
      $fp = new Permissions($file);
      if (!$fp->canRead()) {
         return false;
      }
      $mimeType = $file->getMimeType();
      $fc = Loader::helper('file');
      $fv = $file->getApprovedVersion();
      if ($_GET['file'] == '') {
         header("Location: {$_SERVER['PHP_SELF']}" . $fv->getFileName() . '?file=' . $fv->getFileName() );
         exit();
      }
      $contents = $fc->getContents($file->getPath());
      header("Content-type: $mimeType");


It does feel very hacky, so if anyone has any suggestions of how to improve this I'd been keen to see it.
kranthi20 replied on at Permalink Reply
kranthi20
wow,
It's Very Nice Solution to view or download pdf files.
Hay man jgarcia your each and very reply is very useful to me
Thanks a lot.