Announcement

Collapse
No announcement yet.

[SOLVED] How to download file

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • [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

  • #2
    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.
    Last edited by daveprue; 10-31-2014, 08:56 AM. Reason: clarification
    Dave Prue
    Code Whisperer
    Lahar International Corp
    www.lahar.net

    Comment


    • #3
      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

      Comment


      • #4
        Marking topic as solved.

        Thanks Moelath and dave for your feedback.

        Comment


        • #5
          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...

          Comment


          • #6
            Actually that was a typo should have been docx

            Dave
            Dave Prue
            Code Whisperer
            Lahar International Corp
            www.lahar.net

            Comment


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

              daretzki

              Comment


              • #8
                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
                Dave Prue
                Code Whisperer
                Lahar International Corp
                www.lahar.net

                Comment


                • #9
                  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.

                  Comment

                  Working...
                  X