0

I want to create a website, where user fills out the form and after submitting I want to generate a html file for him with the data he filled and then allow him to download this file.

Is it possible with JS without server side JS?

MazMat
  • 1,904
  • 3
  • 12
  • 24

2 Answers2

3

You can create a link where href property is an encoded URI.

let exampleText ="My Name\nMy Surname\nMy Town\n"
let localfile = "data:text/plain;charset=utf-8," + encodeURIComponent(exampleText);

document.getElementById("linkfile").setAttribute("href", localfile);
<a id="linkfile" download="myInfo.txt">Click here to download</a> 
davidonet
  • 660
  • 4
  • 17
0

If you want to do it JUST frontend side. You could use zip.js to convert those files into a zip, then convert it into a binary array, then convert it into a blob file, and prompt for download using this question.

You may have to casts your html files into DOM "File"s, this might be very slow on mobile since all processing will be done on client side.

Rainb
  • 1,965
  • 11
  • 32