We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27226
    • 186 Posts
    Quote from: doze at Aug 05, 2006, 01:07 PM

    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;


    Hmm. Good. This means to do anything "manually". Why not. But why reading in the file and printing it?
    Providing the filename as the URL should be sufficient, I think.
      • 7923
      • 4,213 Posts
      Quote from: silent at Aug 05, 2006, 03:32 PM

      Hmm. Good. This means to do anything "manually". Why not. But why reading in the file and printing it?
      Providing the filename as the URL should be sufficient, I think.
      To prevent using download managers that bash the servers I guess..


        "He can have a lollipop any time he wants to. That's what it means to be a programmer."
        • 22303 MODX Staff
        • 10,725 Posts
        What I was trying to intersperse in the conversation is that this should be very simple...

        1. add the content type to the manager (see your Configuration, Site Settings tab, towards the bottom)
        2. create a page with a (blank) template (i.e. no template) and set the content type appropriately, as well as the content disposition (also make sure the document is not cacheable, unless you only want to regenerate the content to be downloaded every once in a while, manually)
        3. create a snippet that generates the content of your file and simply returns that content
        4. add the snippet to the document you created’s content area --> e.g. [[MyCSVReport]]

        That’s it. I’ve created completely dynamic, downloadable CSV reports this way, doc files, pdf files, you name it...
          • 2472
          • 151 Posts
          Quote from: OpenGeek at Aug 05, 2006, 03:44 PM

          What I was trying to intersperse in the conversation is that this should be very simple...

          ...

          Just tried it that way and yes I do get a result!

          Snippet on a document with blank template, put a content type ( application/octet-stream for now) and configured page as ’attachment’. Few remarks though:

          - I have two blank lines before my own generated content...
          - every time I click on this document in the manager i get a download dialog, guess that’s the preview stuff... can it be disabled?
          - the document title serves as the name of the file being pushed to the client, right?
            A thing of beauty is a joy forever ( John Keats)
            • 27226
            • 186 Posts
            Yes, Jason, that was exactly the way I first supposed it should work. But when I tried that, there was indeed a file-download prompted in the browser. But the content of that file was html, and the content of that html was an error message obviously intended to be displayed in the MODx-manager (backend) - wasn’t listed in my eventlog though. I didn’t understand that...
            [just see that other posts came in while I’m writing, don’t know what to do right now, so I post anyway, but might delete/edit this one afterwards...]
              • 22303 MODX Staff
              • 10,725 Posts
              Quote from: Avander_be at Aug 05, 2006, 04:25 PM

              Snippet on a document with blank template, put a content type ( application/octet-stream for now) and configured page as ’attachment’. Few remarks though:

              - I have two blank lines before my own generated content...
              - every time I click on this document in the manager i get a download dialog, guess that’s the preview stuff... can it be disabled?
              - the document title serves as the name of the file being pushed to the client, right?

              First, what kind of content does your snippet produce? In other words, is application/octet-stream the appropriate type for content being returned by your snippet?

              Yes, I always disable the Show Preview Window, under Configuration, Miscellaneous; you can always go to the preview tab if you want to see a preview in the manager, but, why? I prefer to just view the actual site outside the manager when I’m working.

              And finally, the alias is used as the filename, unless it is empty, in which case a cleaned version of the pagetitle is used.
                • 22303 MODX Staff
                • 10,725 Posts
                Quote from: silent at Aug 05, 2006, 04:35 PM

                But the content of that file was html, and the content of that html was an error message obviously intended to be displayed in the MODx-manager (backend) - wasn’t listed in my eventlog though. I didn’t understand that...
                What was the error message?
                  • 27226
                  • 186 Posts
                  I’ve seen it with my own eyes. But, since I always provided the same filename, it has been overwritten.
                  And, according to a truism, "a requirement which cannot be budgeted is not a requirement" in this case I just add that "an error which cannot be reproduced is not an error". I must have done something wrong. Just forget about it, sorry lipsrsealed
                    • 27226
                    • 186 Posts
                    I was just looking back and found a strange thing, which, indeed I can reproduce:
                    Trying to find out how Avander_be’s issue might be solved I did the following:
                    1. created a new template which contains nothing but the placeholder [*content*]
                    2. created a snippet [[justtext]] with just one statement:
                    return "comma delimited content";

                    3. created a doc, using the new template and with a document-content of [[justtext]]
                    4. set that doc (in "page settings") to have the content type of "text/plain" and content disposition of "Attachment"
                    When I go to that doc, the browser says that there is a file to be downloaded (doc-title) as ASCII text.
                    And when I download it (takes quite a while), the file has the following content:
                    
                          <html><head><title>MODx Content Manager  » </title>
                    
                          <style>TD, BODY { font-size: 11px; font-family:verdana; }</style>
                    
                          <script type='text/javascript'>
                    
                          function copyToClip()
                    
                          {
                    
                            holdtext.innerText = sqlHolder.innerText;
                    
                            Copied = holdtext.createTextRange();
                    
                            Copied.execCommand('Copy');
                    
                          }
                    
                        </script>
                    
                          </head><body>
                    
                          <h3 style='color:red'>« MODx Parse Error »</h3>
                    
                            <table border='0' cellpadding='1' cellspacing='0'>
                    
                            <tr><td colspan='3'>MODx encountered the following error while attempting to parse the requested resource:</td></tr>
                    
                            <tr><td colspan='3'><b style='color:red;'>« PHP Parse Error »</b></td></tr><tr><td> </td></tr><tr><td colspan='3'><b>PHP error debug</b></td></tr><tr><td valign='top'>  Error: </td><td colspan='2'>explode() [<a href='function.explode'>function.explode</a>]: Empty delimiter.</td><td> </td></tr><tr><td valign='top'>  Error type/ Nr.: </td><td colspan='2'>Warning - 2</b></td><td> </td></tr><tr><td>  File: </td><td colspan='2'>/Applications/MAMP/htdocs/modx/manager/includes/document.parser.class.inc.php(681) : eval()'d code</td><td> </td></tr><tr><td>  Line: </td><td colspan='2'>195</td><td> </td></tr><tr><td> </td></tr><tr><td colspan='3'><b>Parser timing</b></td></tr><tr><td>  MySQL: </td><td><i>0.1004 s s</i></td><td>(<i>44 Requests</i>)</td></tr><tr><td>  PHP: </td><td><i>0.1956 s s</i></td><td> </td></tr><tr><td>  Total: </td><td><i>0.2960 s s</i></td><td> </td></tr></table></body></html>

                    Strange, but reproduceable smiley
                    EDIT: the preview of that doc shows exactly that code. When I click "edit" I see "[[justtext]]" again!
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      I think you would have better luck if you set your document to have a blank template.
                        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