如何使用html将HTML表单数据基本上文件<输入类型= "file"名称= "file"值= " " >发送到桌面



我们正在做一个项目,我们使用表单标签从我们的系统中访问一个文件,然后基本上在桌面上使用html将其下载回我们的系统。

我希望文件保存在桌面上

需要javascript来完成。您必须在输入中获取文件,为该对象创建一个链接,并将链接设置为<a>href。你可以试试下面的代码:

<input type="input" id="inputFile" />
<a id="downloadLink">Download the file</a>
<script>
const fileInput = document.getElementById("inputFile")
const downloadLink = document.getElementById("downloadLink")
fileInput.addEventListener("change", () => {
if (!fileInput.files.length) return;
const urlObj = URL.createObjectURL(fileInput.files[0])
downloadLink.href = urlObj
})
</script>

最新更新