How can I embed the content of a text file attachment into a wiki page?

James Hines March 20, 2012

I have a process that automatically attaches a file to a wiki page, and now I want to embed that text file into the page contents itself. Is there an equivalent to the {include} macro for text file attachments?

6 answers

0 votes
Lee Simpson December 29, 2014

Hi,  its just about 2015 - does anyone know of a solution for this use case?

George Ivanov October 30, 2019

If the question is still actual:

there is a macro (named "SQL Table Transformer") that can do that: see it on Marketplace (https://marketplace.atlassian.com/apps/1219300/sql-powered-table-transformer?hosting=server&tab=overview)

 

Once the macro installed, add the macro container to your desired page. Then you will need to configure it a bit:

1. While editing the page, click the macro container, then click Edit. In the "Input" tab of the macro container configuration enable the "Resolve Wiki Paths in Input URLs" checkbox.

2. Click the "SQL" tab. Replace the default SQL with the following:

SELECT * FROM CSVREAD(':^attached_file_name.ext#0');

or (this will give a slightly different result):

SELECT FILE_READ(':^attached_file_name.ext#0', NULL) "Content";

where: 

  • attached_file_name.ext - is the name of your attached file (here is the assumption is that it is attached to the same page, but it can be also brought from attachments of any space/page as well)
  • #0 - means - read the current latest version of the file.

3. Now, click the "Output" tab and set "Bring Results As" selector to "Table CSV"

4. Try it (click Refresh) in the preview (the container should bring you the contents of the file on your page).

You can play with other settings as well.

5. If satisfied, save the macro configuration and the page.

 

Some SQL Table Transformer macro pages with additional info:

1. general overview: https://v-and-b.atlassian.net/wiki/spaces/PD/pages/229981/SQL+Table+Transformer+Macro+for+Confluence

2. user guide: https://v-and-b.atlassian.net/wiki/spaces/PD/pages/763232257/SQL+Table+Transformer+Macro+for+Confluence+User+Guide

 

Cheers!

George Ivanov October 30, 2019

Here is an example as seen in the macro Preview:attachment content embedding.png

Like andrew.clark-monks likes this
0 votes
James Hines May 15, 2012

Thanks everyone for your suggestions. Charles solution was the closest to what I am wanting to do. I created a user macro as suggested in the forum thread, and the solution is working in Google Chrome. On IE and Firefox it is more problemmatic. Seems like they each handle the <embed> tag slightly differently.

CharlesH
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 16, 2012

If you're only going to be accessing text files then you don't need to restrict yourself to the <embed> tag. An alternative is to use jQuery (client side code) to retrieve the contents of the attachment and insert it into another element, such as a div.

Here's an example to help you: http://stackoverflow.com/questions/6470567/jquery-load-txt-file-and-insert-into-div

You wouldn't need the button-click trigger, just make the ajax call within the (document).ready() block.

I would expect this to overcome any browser specific issues.

I'd be interested in your finished solution if you could post it back here, as would others I expect. :)

Cheers,

Charles

0 votes
CharlesH
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 8, 2012
A similar question has been asked before, and an answer provided at the old forum (http://forums.atlassian.com/thread.jspa?threadID=43212) The solution involves generating a path using some Velocity objects to reference a particular attachment. The browser takes care of accessing the content of the file and rendering it. All of this Functionality is wrapped up in a user macro, so you can re-use it on multiple pages.
Lee Simpson December 29, 2014

Link is broken :(

Mark Devine December 16, 2015

atlassian forums is gone now

0 votes
Renjith Pillai
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 7, 2012

James, rather than attaching to a page, why dont you put the contents itself directly into the page, using the Confluence CLI?

Troy S May 7, 2012

@Renjith, what is confluence CLI?

Renjith Pillai
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 7, 2012

I meant the Confluence Command Line Interface from Bob Swift. https://bobswift.atlassian.net/wiki/display/ACLI/Atlassian+Command+Line+Interface

0 votes
Troy S May 7, 2012

I too would like to see something list the "View File Macro" for text files (http://confluence.atlassian.com/display/DOC/View+File+Macro).

For example:

{viewdoc:name=my document.doc}
{viewppt:name=my presentation.ppt}
{viewxls:name=my spreadsheet.xls}
{viewpdf:name=my document.pdf}

But I would like to be able to do this with a text file like this:

{viewtxt:name=myfile.txt}

I have lots of text file scripts that I want to display in confluence pages, but I don't want to have to edit, copy, and paste every single script. I just want to upload them as attachments to one page and then reference them in other pages. Much easier to upload a text file script after I have changed it than having to edit every single page and do the edit, copy, paste steps.

--- @ James Hines

Would this work around work for you? I know it is a stretch, and I'm not sure if this would produce the results you would want to see, but could you put your content in one cell of an Excel Speadsheet and then use this macro to display the content?

{viewxls:name=Book1.xls|sheet=Sheet1|row=0|col=0}

Good luck...

0 votes
Andrzej Pasterczyk
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 20, 2012

I see the following options:

  1. You can write your own plugin
  2. You can write your own user macro and use it to display iframe with the attached file contents (not sure if that will work - just an idea)
  3. You could change your process to create/update Confluence page instead of attaching file and use excerpt macro whenever you need to include it on a different page.
James Hines March 20, 2012

swiniak,

Thanks for the response, but it really wasn't very helpful. Yes, there are lots of thing that I could do myself, but I was looking for someone that has already solved this problem.

Andrzej Pasterczyk
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 20, 2012

Well... you did not state you wanted a working solution. I just gave some tips on how can you do it (which could be helpfull if you're not too familiar with Confluence)... so now you'll just need to wait for another response or you can start working on it yourself :)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events