samdutton / simpl

Simplest possible examples of HTML, CSS and Javascript:

Home Page:https://simpl.info

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Download video without triggering browser Save As Dialog

greenm1nd3d opened this issue · comments

Hello @samdutton

We don't want to be able to download the video without triggering the browser's Save As Dialog. We want to be able to save the video as soon as "Stop Recording" button is pressed. Calling "download()" on the "stopRecording()" function will still show the Save As Dialog. We just want to save the video to a specified folder and file without prompting the user. How do we achieve this?

Actually, calling the "download()" function on the "stopRecording()" function will trigger this error:

"InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable"

We cannot do this:

function stopRecording() {
mediaRecorder.stop();
console.log('Recorded Blobs: ', recordedBlobs);
recordedVideo.controls = true;
download();
}

Just to clarify: you need to get the Blob as in download():

var blob = new Blob(recordedBlobs, {type: 'video/webm'});

...then save that to the server.

You can't automatically save files to the local file system without the Save As dialog.

You might be able to save to IndexedDB storage, but I haven't tried this.

A possible, if not elegant solution might be to create an anchor (<a>) element which may optionally be hidden from the user. You set the href attribute to an instance of URL.createObjectURL() which you have passed your blob to, then set the download attribute of the anchor to whatever you want the file name to be saved as. Finally invoke the click() method on the anchor and that should do the trick,

I think that the action of automatically saving a file without the 'Save As' dialog is dependent on the user settings. If the user has chosen to use the same location for all downloads, no prompt will be shown. I have confirmed this behaviour using the above-described technique using Chrome 64.

Other more aggressive methods of forcing a download generally result in some kind of warning from the browser, and in any case (and what modern browsers try to enforce) no kind of file transfer to the client should be initiated without client consent.