
Force download script with Miva Script
Written by Claudiu Bischoff, on 3 april 2009
OVERVIEW
Sharing files is cool, but what if you want to force the download (you don't want just to put a link on the page, but to have the file downloaded directly by the user). This is also interesting for those who want to share files directly from DATA location without moving or copying the file.
This small script override the header of the page and change the content-type to "application/force-download".
Tip: Don't forget to add '<miva standardoutputlevel="compresswhitespace">' on top of your file, as Miva Script can add unwanted characters to your content and the downloaded file will be corrupted.
COMPATIBILITY
You must use Miva Empresa 4+ (the miva_output_header and file_read functions aren't available in lower versions of Empresa)
THE CODE
You can see the code here: http://mivascript.org/share/scripts/force_download.mv.txt
How to call the function :
<MvCOMMENT> Variables:
l.filename - The name of the file (it is shown when the download pops up)
l.file - the web path of your file (ex: '/download/myfile.ext)
l.location - Can be SCRIPT or DATA
</MvCOMMENT>
<mvassign name="l.filename" value="{'somefile.mp3'}">
<mvassign name="l.file" value="{'/folder/somefile.mp3'}">
<mvassign name="l.location" value="{'DATA'}">
<mvassign name="l.content" value="{Generate_Header(l.filename, l.file, l.location)}">
<MvIF EXPR="{ l.content:error }">
<MvEVAL EXPR="{l.content:error}"><MvEXIT>
<MvElse>
<MvEVAL EXPR="{l.content}">
</MvIF>
DEMO
Direct download of an image (you will be prompted to download the image):
click here
TODO
Nothing for the moment :-)
COMMENTS
I started a thread in the Miva Merchant (Miva Script section), so don't hesitate: http://extranet.mivamerchant.com/forums/showthread.php?t=20577

