We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 2472
    • 151 Posts
    Hi all,

    I’d like to provided my website visitors with some data stored in the database, the visitor indicates the format ( txt, csv, etc) by clicking a link and receives the requested file in a download dialog, nothing much actually but I keep bumping into walls... sad

    I’ve looked at the FileDownload Snippet but that’s not suitable as i have to generate the files on request... so I tried using an external .php file but then I loose the modx db connection... creating a document in modx gets me in trouble when downloading, I get html instead of my data... tried blank template also but still no cure.

    Any ideas out there on how to solve this in modx?

    TIA
      A thing of beauty is a joy forever ( John Keats)
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You can set the MIME type of the document to be different types; you can add custom types in the Manager - Administration - System Configuration, at the bottom of the Site settings tab.

      You could also make a snippet to get the data, generate and write a file, then "return" a link to the file for downloading.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 2472
        • 151 Posts
        Quote from: sottwell at Aug 04, 2006, 05:41 PM

        ... you can add custom types in the Manager - Administration - System Configuration, at the bottom of the Site settings tab.
        Interesting, I didn’t know one could do that, i’ll investigate, guess I need ’application/octet-stream’...

        You could also make a snippet to get the data, generate and write a file, then "return" a link to the file for downloading.

        Tried something like that, problem here is the ’return a link for downloading’, i don’t see how to do this in php, how do I call this downloading call??

          A thing of beauty is a joy forever ( John Keats)
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          In your snippet, you can do the database query, process the result, write it to a file, then generate a link to the file and return the link:

          // do database stuff, process it, write file myfile.csv to the assets/files folder
          // (that folder will probably need to be world-writable, 777 unless you're running PHP as fastCGI and suexec).
          
          $output = '<a href="assets/files/myfile.csv">myfile.csv</a>';
          return $output;
          


          now wherever you put the snippet that link will show on the page.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            There is a request to register text/csv and text/comma-separated-values, but I don’t know what ever became of it. Properly, the browser should offer to download everything except text/html or text/xhtml, but IE (of course) does not behave properly.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 2472
              • 151 Posts
              Quote from: sottwell at Aug 05, 2006, 02:23 AM

              In your snippet, you can do the database query, process the result, write it to a file, then generate a link to the file and return the link:

              ...


              now wherever you put the snippet that link will show on the page.

              Right, but this requires the user to click to linkagain... while he has already made his choice...

              The tricky part is how to set up the whole process:

              - I have a document showing all available formats in an <ul> with links, no problem.
              - Links point to another document containing a snippet that will generate the flat file in the correct format, no problem either.
              - final step: push the flat file to the visitor, problem: how do you trigger this without intervention??
                A thing of beauty is a joy forever ( John Keats)
                • 27226
                • 186 Posts
                The only solution I could think of would be to use a different template for your snippet-page which uses the body onload feature. The (js) function executed when loading this doc could redirect to your flat file.
                Since the page is displayed after the work of creating the flat file is done and the onLoad event is triggered after the page is loaded, this should work.
                Very basic sample:
                <body onLoad="javascript:window.location.href='data.csv'">


                ("data.csv" of course could be a TV which can provide different URLs, and onLoad should call a self-defined function, not just setting the href-value)

                Hope it helps!
                  • 22303 MODX Staff
                  • 10,725 Posts
                  Simply changing the content disposition from inline to attachment, will send the proper headers so the browser will download the file instead of trying to render it. Is this what you are looking for?

                  I use this all the time to present links to downloadable CSV files, or any other file type I can generate from a MODx page.
                    • 7923
                    • 4,213 Posts
                    Quote from: Avander_be at Aug 05, 2006, 02:50 AM

                    - final step: push the flat file to the visitor, problem: how do you trigger this without intervention??

                    After you have created the file in your snippet, add

                    return $modx->sendRedirect(path/to/your/download.php?fileName=file.txt);


                    Then create the download.php and use this to "push" it for download (snatched from filedownload snippet by Kyle):
                    $filename = $_GET['fileName'];
                    $path = 'path/where/the/file/was/created/';
                    
                    header('Pragma: private');
                    header('Cache-control: private, must-revalidate');
                    if (preg_match('#Opera(/| )([0-9].[0-9]{1,2})#', getenv('HTTP_USER_AGENT')) or
                      preg_match('#MSIE ([0-9].[0-9]{1,2})#', getenv('HTTP_USER_AGENT'))) {
                        header("Content-type: application/octetstream");
                    } else {
                        header("Content-type: application/octet-stream");
                    }
                    
                    $fp = fopen($path.$filename,"r");
                    $filedata=fread($fp,filesize($path.$filename));
                    fclose($fp);
                    
                    if (preg_match('#Opera(/| )([0-9].[0-9]{1,2})#', getenv('HTTP_USER_AGENT')) or
                      preg_match('#MSIE ([0-9].[0-9]{1,2})#', getenv('HTTP_USER_AGENT'))) {
                        header("Content-type: application/octetstream");
                    } else {
                        header("Content-type: application/octet-stream");
                    }
                    header("Content-Disposition: attachment; filename=".$filename);
                    print $filedata;




                      "He can have a lollipop any time he wants to. That's what it means to be a programmer."
                      • 2472
                      • 151 Posts
                      Quote from: OpenGeek at Aug 05, 2006, 12:57 PM

                      Simply changing the content disposition from inline to attachment, will send the proper headers so the browser will download the file instead of trying to render it. Is this what you are looking for?

                      I use this all the time to present links to downloadable CSV files, or any other file type I can generate from a MODx page.

                      Sounds good, I did understand I have to add a custom Mime type to modx first ( according to Sottwell). Could you post an example of how to do it?

                      I guess you have a normal document with links and a receiving document with content disposition = attachment, but how do i refer to the file to download?

                      Thanks all!
                        A thing of beauty is a joy forever ( John Keats)