Here are some ways to allow your users to download the generated screenshots.
If you are proxying ApiFlash's API through your own domain, you can just use an
anchor element
with the download
attribute to allow users to download the generated screenshot.
1 2 3 4 | <a download="screenshot.jpeg"
href="https://yourdomain.com/screenshot&url=https://example.com">
Download
</a>
|
Using ApiFlash's API endpoint https://api.apiflash.com/v1/urltoimage...
directly in the
href
attribute will not work because the download
attribute only
works for same-origin URLs.
If you are not proxying ApiFlash's API yet and would like to use anchor elements to trigger screenshots downloads, please check one of those guides:
As seen previously, the download
attribute of the anchor element only works for
same-origin URLs. But there is still a way to trigger a file download from the frontend using
javascript.
1 2 3 4 5 6 7 8 9 10 11 12 | fetch('https://api.apiflash.com/v1/urltoimage?access_key=YOUR_ACCESS_KEY&url=https://example.com')
.then(response => {
response
.blob()
.then(blob => {
let a = document.createElement('a');
a.href = window.URL.createObjectURL(blob);
a.download = 'screenshot.jpeg';
document.body.appendChild(a);
a.click();
});
});
|
In the previous example we make an api call to ApiFlash's screenshot API and get the response as a blob. Then we create an anchor element and simulate a click to trigger the file download. We used the Fetch API , but you can use anything you'd like to make the HTTP request to perform the api call.
If the HTTP response contains the Content-Disposition
header with the
attachment
parameter,
almost all browsers will automatically trigger a file download.
1 | Content-Disposition: attachment; filename="filename.jpg"
|
When your user is navigating to one of your endpoint, you can simply make an API call to ApiFlash
from the backend, put the resulting image in the HTTP response's body and set the
Content-Disposition
header.