Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript “自动”;下载「;提交表格时的PDF格式_Javascript_Html - Fatal编程技术网

Javascript “自动”;下载「;提交表格时的PDF格式

Javascript “自动”;下载「;提交表格时的PDF格式,javascript,html,Javascript,Html,我有一个表单按钮,当点击它提交表单 我希望同时,浏览器开始下载PDF文件。我想知道我该怎么做?请记住,默认情况下PDF通常由浏览器打开,但我希望浏览器“另存为”文件,而不是打开文件。这只能使用html还是我需要javascript?在这种情况下,您必须配置Web服务器以强制下载PDF。 如果您使用的是Apache,不妨看看这篇文章-> 或者,如果您不必支持所有浏览器,您可以不使用html5下载属性->在这种情况下,您必须将Web服务器配置为强制下载PDF。 如果您使用的是Apache,不妨看看

我有一个表单按钮,当点击它提交表单


我希望同时,浏览器开始下载PDF文件。我想知道我该怎么做?请记住,默认情况下PDF通常由浏览器打开,但我希望浏览器“另存为”文件,而不是打开文件。这只能使用html还是我需要javascript?

在这种情况下,您必须配置Web服务器以强制下载PDF。 如果您使用的是Apache,不妨看看这篇文章->


或者,如果您不必支持所有浏览器,您可以不使用html5下载属性->

在这种情况下,您必须将Web服务器配置为强制下载PDF。 如果您使用的是Apache,不妨看看这篇文章->


或者,如果您不必支持所有浏览器,您可以只使用html5下载属性->

选项1:您可以让表单提交到的url返回下载

选项2:使用ajax调用提交数据onclick,将window.location设置为下载url


无论pdf是在浏览器中打开还是下载,都是您无法控制的客户端设置。

选项1:您可以让表单提交到的url返回下载

选项2:使用ajax调用提交数据onclick,将window.location设置为下载url


无论pdf是在浏览器中打开还是下载,都是无法控制的客户端设置。

如果您在服务器端使用PHP,请尝试此方法。这是在我的一个网站上运行的经过测试的代码

<?php
    ob_start();
    $file = 'YOUR-FILENAME-HERE.pdf';

    if (file_exists($file)) 
    {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit();
    }
?>


浏览器将提示下载而不在浏览器中显示。

如果您在服务器端使用PHP,请尝试此操作。这是在我的一个网站上运行的经过测试的代码

<?php
    ob_start();
    $file = 'YOUR-FILENAME-HERE.pdf';

    if (file_exists($file)) 
    {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit();
    }
?>

浏览器将提示下载而不在浏览器中显示。

您可以使用javascript下载文件。
否则,在javascript中,您可以为其编写代码

$('a').click(function(e) {
   e.preventDefault();  //stop the browser from following
   window.location.href = 'downloads/file.pdf';
});

<a href="no-script.html">Download now!</a>
$('a')。单击(函数(e){
e、 preventDefault();//停止浏览器执行以下操作
window.location.href='downloads/file.pdf';
});
或者,你可以用这个

在PHP中:

<?php
  header('Content-type: application/pdf');
  header('Content-disposition: attachment; filename=filename.pdf');
  readfile("file.pdf");
?>

在Javascript中:

<body>
<script>
 function downloadme(x){
    winObj = window.open(x,'','left=10000,screenX=10000');
    winObj.document.execCommand('SaveAs','null','download.pdf');
    winObj.close();
 }
 </script>

 <a href=javascript:downloadme('file.pdf');>Download this pdf</a>
 </body>

函数下载我(x){
winObj=window.open(x,,'left=10000,screenX=10000');
execCommand('SaveAs','null','download.pdf');
winObj.close();
}
您可以使用javascript下载文件。
否则,在javascript中,您可以为其编写代码

$('a').click(function(e) {
   e.preventDefault();  //stop the browser from following
   window.location.href = 'downloads/file.pdf';
});

<a href="no-script.html">Download now!</a>
$('a')。单击(函数(e){
e、 preventDefault();//停止浏览器执行以下操作
window.location.href='downloads/file.pdf';
});
或者,你可以用这个

在PHP中:

<?php
  header('Content-type: application/pdf');
  header('Content-disposition: attachment; filename=filename.pdf');
  readfile("file.pdf");
?>

在Javascript中:

<body>
<script>
 function downloadme(x){
    winObj = window.open(x,'','left=10000,screenX=10000');
    winObj.document.execCommand('SaveAs','null','download.pdf');
    winObj.close();
 }
 </script>

 <a href=javascript:downloadme('file.pdf');>Download this pdf</a>
 </body>

函数下载我(x){
winObj=window.open(x,,'left=10000,screenX=10000');
execCommand('SaveAs','null','download.pdf');
winObj.close();
}

您是否介意发布一些代码(即使只是从本文中复制代码)?链接到其他文章本身并不是一个正确的答案。你介意发布一些代码吗(即使只是从这里的文章中复制代码)?链接到其他文章本身并不是一个正确的答案。