0

I wonder is there a way to override save file dialogue, while saving file in javascript.

I have a file (html or text or image) that is saved in localstorage. I would like to download it, but to set its download name to whatever I need. Right now it works, but it defaults to download (name) and file type (all).

This is the code that allows me to selectively pick whatever I need from local storage, but as I described above I have issue with name. I can't use server for this, as this is standalone Backbone project.

var base64 = window.btoa(this.model.get("filecontent")),   
fileDownLoadInformation;

fileDownLoadInformation = "<a href='data:application/octet-stream;base64," + base64 + "'" +   
">Download</a>"

//All file type have these options
extraOptions = "<div>" + fileDownLoadInformation + "</div>";

//etc...

1 Answer 1

1

If you want to create downloadable file from JavaScript I think you could use Blob API. See at HTML and you'll find "download" attribute on link, that will be the filename. Then you just need to change the content source from textarea to localStorage. I hope it could help you. Cheers.

HTML:

<textarea id="content">woo hoo! I'm going to be in a file!</textarea>
<p><a href="#" id="link" download="whatever.txt">download me</a></p>

JavaScript:

$('#content').on('change', function(){
    var $this = $(this);

    //this is OK for our example code but should be throttled in production
    var _downloadUrl = URL.createObjectURL(new Blob([$this.val()] , {type:'text/plain'}));

    $('#link').attr('href', _downloadUrl);
}).change();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.