Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iframe downloading #37

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions features/download.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Download</title>
</head>
<body>
<p><a href="../index.html">[Home]</a></p>

<p>Download PDF</p>
<ul>
<li><a href="download.pdf" target="_blank">Open PDF _blank</a>
<li><a href="download.pdf" download target="_blank">Download PDF _blank</a>
<li><a href="download.pdf">Open PDF</a>
<li><a href="download.pdf" download>Download PDF</a>
</ul>

<p>Download via iframe</p>
<ul>
<li><a href="#" class="init-iframe-download" data-type="json" >Init iframe JSON load</a>
<li><a href="#" class="init-iframe-download" data-type="pdf" >Init iframe PDF load</a>
</ul>
<div id="iframe-container"></div>

<script>
const iframeContainer = document.getElementById('iframe-container');
const initIframeDownloadLinks = document.querySelectorAll('.init-iframe-download');
[...initIframeDownloadLinks].forEach((link) => {
link.addEventListener('click', () => {
const iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.src = `./download/${link.dataset.type}`;
iframeContainer.appendChild(iframe);
});
});
</script>

</body>
</html>
21 changes: 0 additions & 21 deletions features/pdf-download.html

This file was deleted.

2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ <h2>Browser Features</h2>
<li><a href="./features/url-schemes.html">URL Schemes</a></li>
<li><a href="./features/stack-tracing/">Stack tracing</a></li>
<li><a href="./features/canvas-draw.html">Canvas draw</a></li>
<li><a href="./features/pdf-download.html">PDF download</a></li>
<li><a href="./features/download.html">Downloads</a></li>
</ul>

<h2>Security</h2>
Expand Down
26 changes: 26 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,29 @@ app.get('/security/csp-report/reports', (req, res) => {
cspIds.delete(req.query.id);
}
});

function downloadPDF (res) {
res.download('./features/download.pdf');
}

function downloadJSON (res) {
const json = JSON.stringify({ example: 'test' });
const buf = Buffer.from(json);
res.writeHead(200, {
'Content-Type': 'application/octet-stream',
'Content-disposition': 'attachment; filename=data.json'
});
res.write(buf);
res.end();
}

app.get('/features/download/:type', (req, res) => {
switch (req.params.type) {
case 'json':
downloadJSON(res);
break;
case 'pdf':
downloadPDF(res);
break;
}
});