[SOLVED] How to download file

Dear Friends,

I have field with Document(file name) type in the grid.
with this type, user can click this field and SC allowed to download the file directly. It can run in my grid perfectly.
but what i need is, i need to change the file name first before file downloaded.
example :
File id in db = 10
file name = readme.docx
then, when uploading, i rename this file in the server into : 10.dat

so, when i list it in the grid, it is showing file name in db : readme.docx ( i took filename from db value).
but when user click the file name in the grid, first i need to search file 10.dat in server,
and rename it to readme.docx, after that download it.

how can i interrupt on click event while using document(file name), so i can add script to rename file in the server first before download ?

or if it is cannot be done, i plan to have my “download” button.
but how to create a download script in SC ?
anyone can share it to me ?
sorry, i am a newbie in PHP scripting.

thanks
pamulat

sc version : 8.0.014

moelath,

This is how I would do it:

First, make a blank app, and enter the onExec code like this:

//---------------------------------------

header(‘Pragma: public’);
header(‘Expires: 0’);
header(‘Cache-Control: must-revalidate, post-check=0, pre-check=0’);
header(‘Cache-Control: private’, false); // required for certain browsers
header(‘Content-Type: application/docx’);

header(‘Content-Disposition: attachment; filename="’. [dl_filename] . ‘";’);
header(‘Content-Transfer-Encoding: binary’);
header('Content-Length: ’ . filesize([filename]));

readfile([filename]);

//--------------------------------------

example to call it:
sc_redir(blank_app,filename=’/my/path/to/file/10.dat’;dl_filename=‘readme.docx’);

Then you can either make a link of type field that calls the blank app (you need to pass filename and dl_filename as parameters of course) or, if you need to set up some things before the redirect then you can create a php button that executes some code and then does a redir to the blank app. You might even want to pass a global that contains the name of an app to redirect to after the download.

There is no need to rename the file on the server when downloading.

Thanks it works!

Hi Dave Prue,

Thanks! its great!
it works as expected. I really appreciate it.
you (and of course SC) save my days … :-).

warm regards,
moelath

Marking topic as solved.

Thanks Moelath and dave for your feedback.

This will fail on IE7 and below on certain filenames (check with + & and # characters in the filename).
I dont understand the header(‘Content-Type: application/docxf’) part tho, that is not a registered extension so it will only be directly downloadable. Interesting…

Actually that was a typo :slight_smile: should have been docx

Dave

Is [filename] in example equal to {filename} field in Application set as Document(blob/database)? Right?
Thanks davepru. Nice hint.

daretzki

Actually, in the original question he was asking how to download a renamed file, so it would be the name as it was stored on the server. In my example, it would be a global variable that you would pass as a parameter to the sc_redir() macro.

In my example, the 1st use of [filename] would be just the name of the file - this is the filename that your browser wants to save the file as, and the 2nd and 3rd uses of [filename] should actually be the path & filename of the file on the server. They can be different if you want them to.

Dave

Hello , I need to download two files file1.txt and file2.txt , how I can do this?
When I download the first system stops and does not lower the second.

I know this is an old post but…what am I doing wrong? I have tried several variations of this, including the path I would use in production, but why does this not work? It downloads a file of the right name but it is effectively blank (see below for the first few lines, which I read by renaming the file zip to txt.) This code is in a PHP Button.

$ubot_name = "boty.zip";  
$ubot = 'C:\zampp\htdocs\html\vendor\soundasleep\html2text\bot.zip';// there is a file of that name there!
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false); // required for certain browsers
header('Content-Type: application/zip');

header('Content-Disposition: attachment; filename="'. $ubot_name  . '";');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($ubot));

readfile($ubot);

exit;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">

<html DIR='LTR'>
 <head>
    <META http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <link rel="shortcut icon" href="../_lib/img/scriptcase__NM__img__NM__ct_pie_donut_2d.png">
    <SCRIPT type="text/javascript">
      var sc_pathToTB = '/scriptcase/prod/third/jquery_plugin/thickbox/';
      var sc_tbLangClose = "Close";
      var sc_tbLangEsc = "or Esc key";

Attention, every file may be downloadable this way, possibly also unwanted php source code or password files or other system files.