How can I embed the content of a text file attachment into a wiki page?
Asked by James Hines:

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?


Answers:
author: Lee Simpson
body: Hi, its just about 2015 - does anyone know of a solution for this use case?

comments: author: George Ivanov
body: 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](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!


author: George Ivanov
body: Here is an example as seen in the macro Preview:![attachment content embedding.png](https://community.atlassian.com/t5/image/serverpage/image-id/71130iA49B509BBB9CA9D1/image-size/large?v=v2&px=999 "attachment content embedding.png")


author: James Hines
body: 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.

comments: author: CharlesH
body: 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


author: CharlesH
body: 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.

comments: author: Lee Simpson
body: Link is broken :(


author: Mark Devine
body: atlassian forums is gone now


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

comments: author: Troy S
body: @Renjith, what is confluence CLI?


author: Renjith Pillai
body: I meant the Confluence Command Line Interface from Bob Swift. <https://bobswift.atlassian.net/wiki/display/ACLI/Atlassian+Command+Line+Interface>


author: Troy S
body: 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...


author: Andrzej Pasterczyk
body: 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.

comments: author: James Hines
body: 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.


author: Andrzej Pasterczyk
body: 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 :)