1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

XF 1.1 Force certain attachment to view/open directly on the browser instead of downloading it

Discussion in 'XenForo Questions and Support' started by RoldanLT, Jul 14, 2013.

  1. RoldanLT

    RoldanLT Well-Known Member

    Just like image(jpg,png,gif) attachment, if you click them on the attachment, you can view it directly.
    So I want certain extension also, like .txt to open directly on the browser when click not to download.

    How can I do that?
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    That is up to the client browser. You can only control your own browser settings.
    RoldanLT likes this.
  3. RoldanLT

    RoldanLT Well-Known Member

    Any way I can force it display via htaccess?
  4. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member


    The way to approach this would be to create an addon that changes the direct link into a web page that displays the content of the text file. Gotta post a request:


    Otherwise with the direct link you are subject to the client browser's handling of different content types.
  5. Mike

    Mike XenForo Developer Staff Member

    Changing this behavior can lead to a potential security issue (in some versions of IE).
    RoldanLT likes this.
  6. AlexT

    AlexT Well-Known Member

    Not necessarily. This is where MIME come into play, specifically the Content-Disposition and Content-Type headers.

    For example, depending on your installed PDF viewer,

    Content-Disposition: inline; filename="document.pdf"

    will open the PDF inline for viewing inside the browser window. Whereas

    Content-Disposition: attachment; filename="document.pdf"

    will inform the browser that the PDF should be saved as a file instead. Sure, ultimately it depends on your client and what apps you have installed on your client computer, but there is at least a way to hint the browser of how the file should be opened/downloaded. Not every browser behaves the same, and here is a (somewhat outdated) list with example links of how browsers react to sending inline txt content:

    AndyB and RoldanLT like this.
  7. erich37

    erich37 Well-Known Member

  8. AlexT

    AlexT Well-Known Member

    You do this via http headers. Either in your webserver configuration, if you have access, or via software. Unlike vBulletin, xF doesn't allow to change mime settings for individual attachment types, so that would require an add-on.
    AndyB and erich37 like this.
  9. Mick West

    Mick West Well-Known Member

  10. AndyB

    AndyB Well-Known Member

    Mick West and RoldanLT like this.
  11. Mick West

    Mick West Well-Known Member

    Hah, if I'd seen that I'd not have bothered writing mine, it's the same idea. :)

    I think my approach is slightly cleaner though, as I always call parent::renderRaw, so there's less duplication of code. Plus it handles multiple mime types now.
    RoldanLT likes this.
  12. stacy.c

    stacy.c Member

    Is there a way to force a link to a file in a post to download rather than view inline?

    I am helping a client on XF 1.4 to create a link to a PDF within a post and make the default link behavior to download the file.

    I know that adding it as an attachment will accomplish this but he wants to do it in the post.


Share This Page