flo*_*olu 1 html javascript url file download
目前,我正在使用a
和href
。但我不希望客户端能够获取 URL。
<a href="URL">Download</a>
Run Code Online (Sandbox Code Playgroud)
因此,我只是想问如何通过 Javascript 启动文件下载。(假设我有可用的网址)
创建一个元素而不将其附加到任何父元素,这样用户就看不到它,然后在 JS 中单击它。
link = document.createElement("a"); //create 'a' element
link.setAttribute("href", "file"); //replace "file" with link to file you want to download
link.setAttribute("download", "file");// replace "file" here too
link.click(); //virtually click <a> element to initiate download
Run Code Online (Sandbox Code Playgroud)
如果客户端愿意,仍然可以在 javascript 代码中看到 URL,但链接是不可见的。