Skip to content

Instantly share code, notes, and snippets.

@daijinload
Created September 9, 2017 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daijinload/094a4df1d8853276b10539d882666a2c to your computer and use it in GitHub Desktop.
Save daijinload/094a4df1d8853276b10539d882666a2c to your computer and use it in GitHub Desktop.
postして、帰ってきたものをダウンロードしちゃうというね。文字列でもおk。レンダリングしないから動作的には軽いのさ。
function downloadData(content, filename, mimetype) {
// debugger;
if (!mimetype) {
mimetype = 'application/octet-stream';
if (typeof content === 'string') {
mimetype = 'application/octet-stream;charset=UTF-8';
}
}
var url = (window.URL || window.webkitURL).createObjectURL(new Blob([content], { 'type': mimetype }));
var a = document.createElement('a');
var evt = document.createEvent('MouseEvents');
a.target = '_blank';
a.download = filename || 'aaa.txt';
a.href = url;
evt.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
a.dispatchEvent(evt);
}
downloadData('みくだよーさん')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment