根据您的文件类型,修改类型以适应,以下是MIME类型列表
https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
要将文件下载为html,请将类型修改为text/html
<form action="/a" method="POST">
<input
type="text"
value="String"
name="string"
id="filename"
/>
<input type="submit" value="Submit" />
</form>
<script>
document.querySelector("form").addEventListener("submit", (e) => {
e.preventDefault();
const string = document.querySelector("#filename").value;
const element = document.createElement("a");
const file = new Blob([string], { type: "text/plain" });
element.href = URL.createObjectURL(file);
element.download = "test.txt";
document.body.appendChild(element); // Required for this to work in FireFox
element.click();
});
</script>