REST API: Download File from repository and show in brower

Hello,
it is actually not a KNIME server problem, but maybe someone can help me.
I use a workflow to save some pdf files in the KNIME repository.
I would like to link these files on another website (access by REST API).
HTML:
<a href="http://ServerAdress:Port/knime/rest/v4/repository/data/test.pdf:data" target="_blank"> Show PDF xyz </a>
The problem is that the pdf file is downloaded and not displayed in the browser tab.
Can I open the file with HTML / JavaScript in the browser?
Greetings

Hi @Tim8200 , I think this might be more on the client side (browser) than on the server side. The browser has to be able to open pdf to start with - this is not something that a browser can do by default, you need to install some plugins/extensions (the browser plugins usually get installed when installing a pdf reader).

The next thing to look at is how the browser is configured. For security reasons, the browser might be set to download the pdf file instead of opening it.

Hello @bruno29a,
thank you for quick reply.
But i don’t know how to configure the browser because it works fine with any other link e.g.:
https://www.knime.com/sites/default/files/KNIME%20Analytics%20Platform%20Course%20for%20Beginners.pdf

HTML:
<a href="https://www.knime.com/sites/default/files/KNIME%20Analytics%20Platform%20Course%20for%20Beginners.pdf" target="_blank" >PDF Anzeigen</a>

Greetings

Hi @Tim8200 , ok, then your browser is already configured.

One thing that I used to do in PHP several years ago was to control the http header on the server that allowed control on whether to download or to open (stream) a file on the client side. I suppose that Knime server should be able to send these http headers as well - would need someone from the Knime team to confirm. The http headers that would usually do it would be:
'Content-Type: application/pdf'
'Content-Disposition: inline; filename="file.pdf"'

The “inline” Disposition as opposed to “attachment” would allow the browser to open the file instead of downloading.

Edit: Any reason why you are appending “:data” at the end of the url?

Hi @bruno29a ,
appending “:data
that’s what it says in the KNIME REST documentation
image

image
May be that’s why it “Downloads” it? :slight_smile:

Can you try without the “:data”?

Hi,
of course I have tried it without the :data :slight_smile:
doesn’t work.
I know that the file is being “downloaded”.
The question was rather how can i open the downloaded file in the browser using html / javascript.
i have found a solution, not very nice but seems to work.

<script>
const url2 = 'http://Server:Port/knime/rest/v4/repository/somedata.pdf:data'
var xhr = new XMLHttpRequest();
xhr.open('GET', url2);
xhr.responseType = "blob";
xhr.onload = () => {
	const file = window.URL.createObjectURL(xhr.response);
	window.open(file,'_blank')
}
xhr.send();
</script>
3 Likes

Hey @Tim8200 , I think your solution is nice actually, this javascript is a very good solution.