Skip to content

Instantly share code, notes, and snippets.

@rwbrockhoff
Last active March 21, 2019 14:02
Show Gist options
  • Save rwbrockhoff/fc133dc415b580ff5b7665cd31ba93bf to your computer and use it in GitHub Desktop.
Save rwbrockhoff/fc133dc415b580ff5b7665cd31ba93bf to your computer and use it in GitHub Desktop.
//Frontend Download Method
downloadVideo = () => {
client({
url: 'http://localhost:3001/feed/download',
method: 'GET',
responseType: 'blob', // important
}).then((response) => {
//Repsonse.data does have video file and opening url for non-iOS browsers it works fine
const setWindow = window.URL || window.webkitURL;
//Per Google documentation on blob and deprecated blobBuilder
const url = setWindow.createObjectURL(new Blob([response.data],
{ type: "application/force-download" }));
//I've tried the proper video MIME type: video/quicktime
//application/force-download
//application/octet-stream
//I tried adding a link element to the dom to simulate an event (which I've read is necessary for Safari to open content, even beyond the click into this download func)
//I tried window.replace(url);
//I tried window.location.href = url;
//I tried new tab, self, blank
//I tried FileSaver.js, which also mentions it can't do it for iOS in the documentation
//Safari natively doesn't have a way of handling blob data urls.
//FileReader() API essentially mimics the <a download> that's currently in place
});
}
//Backend Endpoint
async function downloadVideo(req, res, next) {
try {
const fileUrl = 'https://a41********fadfa.cloudfront.net/************-4*AF-8*53-********75da6F.MOV';
var passThrough = through2();
res.set('content-disposition', 'attachment');
res.set('content-type', 'video/quicktime')
request.get(fileUrl).pipe(passThrough).pipe(res);
} catch (error) {
next(error);
}
}
//Does work as expected. Proper headers are set and file retrieved on frontend as blob.
//You can add an attachment filename, which I had tried for most of the time yesterday.
//I experimented with not having that in the code, as well as trying the other MIME types listed in the
//download function from the frontend.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment