A forcedownload solution (Resource Links that don't open a new window)

A forcedownload solution (Resource Links that don't open a new window)

by Logan Modine -
Number of replies: 4
Hi,

I was looking around the forums for a solution to getting my .pdf resource links to automatically download instead of opening a popup window with a download link (I'm using Moodle 1.8.4) when I came up with my own solution which I'd like to share.

There were several suggestions that involved modifying file/filelib.php, usually having to do with the lines

$mimetype = 'application/x-forcedownload';
$forcedownload = true;
$lifetime = 0;

but none seemed to work.

One fix that did correct the downloading behavior was to add ?forcedownload=1 to the end of each resource link url, but this seemed tedious to implement or remove. A site that already has many resource links created would require a lot of time to modify in this way, and there were several reports on the forums of this simply not working.

Instead, I edited the file mod/resource/type/file/resource.class.php

On line 218 (or thereabouts if using a different Moodle version) there's the line

if ($resource->options != "bogusoption_usedtobe_frame") {

followed by a listing of the $mimetype's available.

In my case, since I wanted my .pdf files do download straight away instead of creating a popup window, I modified the $embedded option in the "else if" statement to read false instead of true:

else if ($mimetype == 'application/pdf' || $mimetype == 'application/x-pdf') {
$resourcetype = "pdf";
$embedded = false;
}

This changed the behavior of my resource links for .pdf file globally, and is easy enough to change back if needed. I've tested this on Firefox and IE7 on Windows XP and Firefox on OSX and it seems to work fine.

If you're looking for a solution to getting your links to automatically download instead of creating an intermediary popup window and have been unsuccessful with modifications to file/filelib.php, give this a shot.
Average of ratings: Useful (1)
In reply to Logan Modine

Re: A forcedownload solution (Resource Links that don't open a new window)

by Geoffrey Rowland -
Picture of Plugin developers
Seems to do the trick! Thanks cool
In reply to Logan Modine

Re: A forcedownload solution (Resource Links that don't open a new window)

by Gain Oloya -
Yeah, thanks too for sharing.
In reply to Logan Modine

Re: A forcedownload solution (Resource Links that don't open a new window)

by Jonathan Taylor -
This is very good info! Thanks.

My problem however, is the opposite. I want files (PDFs, mp3s, video files, etc) to automatically open/play in a popup window. For the life of me, I can't figure out how. I have everything set to $embedded = true; but the behavior I get is very erratic.

For example, with Safari in OS X, mp3 resource files once played in a popup window with Quicktime. Now when the window opens, I get the QT logo with a question mark over it. The same resource plays fine in Firefox under OS X. But in Windows, both Firefox and IE force download. This is so frustrating. How can I make the behavior more regular and universal. On top of this, some users are forcefully logged out when trying to open PDF files but only on certain computers and both on Macbook. PDFs should just open with Apple's built in Preview. This is really getting annoying. Also, on some platforms with certain browsers, if you right click the resource and try a save as, the file is downloaded as a php or html file.

Any help is so gratefully appreciated.
In reply to Jonathan Taylor

Re: A forcedownload solution (Resource Links that don't open a new window)

by K Lewis -
Thanks for the info. This helped me with a similar pdf issue. I am just beginning to learn the ins and outs of php and am doing so on the fly which kind of how I like it anyway. Moodle and other open source programs are the best for this.