download video in typescript

To download a video in TypeScript, we can make use of the Fetch API and the Blob API.

Here is an example code snippet:

index.ts
async function downloadVideo(url: string) {
  const response = await fetch(url);
  const blob = await response.blob();
  const urlObject = window.URL.createObjectURL(blob);
  const a = document.createElement('a');
  a.href = urlObject;
  a.download = 'video.mp4';
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
}
343 chars
12 lines

In the example above, we first make a call to the fetch method passing in the URL of the video we want to download. We then convert the response into a Blob and create a new URL object using the URL.createObjectURL method.

Next we create a new anchor element and set the href and download attributes to the URL object and the name of the file respectively. We then append the anchor element to the document and trigger a click event to start the download. Finally, we remove the anchor element from the document.

gistlibby LogSnag