有没有办法“强制”浏览器下载文件而不是打开文件?
<a href="file.txt">Download this file</a>
我已经使用 window.open("file.txt", "Download"); 通过 js 尝试了该方法;
但没有成功。
谢谢。
更新:
我做了一个php文件如下;
<html>
<a href='dl.php?bid=3'>
<php>
$sql="select barquivo from bibilioteca where bid=$_GET[bid]";
$row=mysql_fetch_assoc(mysql_query($sql));
header("Content-Disposition: attachment;filename=biblioteca/$row[barquivo]");
然后它下载一个 0 字节的文件“biblioteca_”。
最佳答案
您应该在服务器端执行此操作。
如果你发送一个
Content-type: application/octet
或
Content-disposition: attachment; filename=file.txt
header 然后会提示用户下载。
关于用于下载 txt 文件的 Javascript JQuery 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1882459/