We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17723
    • 221 Posts
    Hello,
    Can’t find this snipped for modifying links to be download instead of opened. It’s described in documentation but the link to the snipped doesn’t show anything http://rtfm.modx.com/display/revolution20/Input+and+Output+Filters+%28Output+Modifiers%29#InputandOutputFilters%28OutputModifiers%29-OutputModifiers

    Does any one have this snipped and can share?

    Thanks,
    Lucas

      Lucas from AroundCyprus.net
      • 6228
      • 249 Posts
      I’m not sure I understand your request correctly, but if you mean, have pages be delivered as downloads rather than be displayed in a browser window, you need to set the [tt]content-disposition[/tt] property in the page settings tab to ’download’. You can do this with any document.
        lo9on.com

        MODx Evolution/Revolution | Remote Desktop Training | Development
      • It should be added that if you have a page set to be downloaded instead of displayed in-line, it is ultimately up to the browser as to how a particular header is handled. E.g. if Chrome doesn’t have a PDF plugin installed and it encounters an "in-line" PDF Static Resource, it will download the PDF instead of opening it. Snippets don’t control how a page is handled because they aren’t controlling the headers.
          • 17723
          • 221 Posts
          OK. Than in my case ... what should I do... huh

          I have a TV (file) that has a file name of my pdf. In my template i have [[*content]] area with text description an a link to download a PDF document: <a href="[[*TVfile]]" target="_blank">get pdf</a>.
          I don’t want to change content disposition for my document to attachment because the document itself has content area with text that people want to read.
          The only thing that I want to is to force download instead of opening pdf file when clicked on it.

          Luke
            Lucas from AroundCyprus.net
          • I think we’re saying the same thing, but just to be sure... You can just create a MODx Static Resource, choose the PDF file as the source file, ensure that the content type is PDF (you may have to add your own content type), set the "content disposition" to either inline or attachment, then just link to the page (i.e. the Resource) normally as you would any other MODx page, e.g.

            <a href="[[~123]]">get pdf</a>


            Where "123" is the page id of the Static Resource.

            So if you’re storing a link to that resource in a TV (e.g. a TV that allows you to select another page), then you would select the Static Resource page when creating/editing pages that used that TV. Inside your template, your link would be generated by referencing the TV placeholder:

            <a href="[[~[[*myTv]]]]">get pdf</a>


            So [[*myTv]] would get converted to the page id (e.g. 123), and then the ~ placeholder would convert to that page’s URL. Make sense?
              • 17723
              • 221 Posts
              Hi Everett,
              thank You for sharing Your solution. Yes Your approach will surely work, but I want to make it easier, when managing the content. Actually for each document i have text and a pdf file. It’s much easier to save pdf in TV in this document, than first crating static resource, and that linking it with that document.

              But You give me one more idea that i need to somehow workout. If I can crate a static resource eg. id=123, and pass to it the value of myFileTV, and than call it in my template eg <a href="[[~123]]?content=myTv">get pdf</a> than I would not need to crate static resources for each of my pdf that are alredy stored on server in myTv.

              If You have any idea how to pass value of myTv to content of static resource please advise. I’ll still try to find a way to do it.

              Regards,

              Lucas.
                Lucas from AroundCyprus.net
              • Hmm... I don’t think what you’re thinking will work. It sounds too much like rubbing a cat backwards.

                Why not do this? Create a dropdown TV, then use the @DIRECTORY binding to create a list of files in that directory, allowing the user to select one. Just make sure to only put PDFs in that directory. Then you can just link to the file directly, e.g.

                <a href="[[*myTV]]">download PDF</a>


                You don’t really need to create Static Resources unless you need to control access to those files and/or associate additional meta data with them.
                  • 17723
                  • 221 Posts
                  Quote from: Everett at Jul 01, 2011, 10:49 PM

                  You don’t really need to create Static Resources unless you need to control access to those files and/or associate additional meta data with them.
                  Yes, that’s my case I need to set headers to force download instead of opening the file.
                    Lucas from AroundCyprus.net
                  • Good catch on the docs there - either the example needs to be changed, or a snippet added somewhere.

                    What a snippet like that would need is:
                    - a connector/resource that triggers the download (securely, not allowing to download files like core/config/config.inc.php for example wink )
                    - create a link to that connector/resource.

                    Let’s assume all your downloadable files are in assets/downloads. I think that if you set one resource with content disposition: attachment, give it a blank template and in the content put a [[!Download]] snippet call, you could get away with a snippet like this:

                    $file = $_GET['f']; // assumes we're passing ?f=my/file/here.pdf
                    $path = MODX_ASSETS_PATH . 'downloads/';
                    if (file_exists($path.$file)) {
                      $f = file_get_contents($path.$file);
                      return $f;
                    }
                    return 'Could not find file.';
                    


                    I didn’t test this and it would probably need to be modified before being production-worthy, but perhaps it can help you figure out how to make this work.
                      Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                      Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                    • force download instead of opening the file.

                      You don’t have full control over that: the browser gets final say. So if you have a PDF and you want to force a download so you set it to content-disposition of "attachment", I’ve still have some browsers open it up in-line. I’d say it’s a lot easier to offer links to the files directly and leave it to the browser to open or download. The users can always do a "Save As" if the browser opens it up in-line.