我有一些大尺寸的PDF目录在我的网站上,我需要链接这些下载。当我在谷歌上搜索时,我发现下面有这样一件事。它应该打开“另存为…”弹出链接点击…

 <head>
    <meta name="content-disposition" content="inline; filename=filename.pdf">
    ...

但它不工作:/当我链接到一个文件如下,它只是链接到文件,并试图打开文件。

    <a href="filename.pdf" title="Filie Name">File name</a>

更新(根据以下答案):

据我所知,没有100%可靠的跨浏览器解决方案。也许最好的方法是使用下面列出的web服务之一,并提供下载链接…

http://box.net/ http://droplr.com/ http://getcloudapp.com/


当前回答

一个非常简单的方法来实现这一点,不使用外部下载网站或修改标题等,只是创建一个ZIP文件与PDF里面,并直接链接到ZIP文件。这总是会触发保存/打开对话框,人们仍然很容易双击PDF窗口,与.zip相关的程序被启动。

顺便说一句,这是个好问题,我也在寻找答案,因为大多数浏览器内置的PDF插件需要很长时间才能显示任何东西(并且在PDF加载时经常会挂起浏览器)。

其他回答

使用download属性,但要考虑到它只适用于与您的代码位于同一源中的文件。这意味着用户只能下载来自源站点、同一主机的文件。

用原始文件名下载:

<a href="file link" download target="_blank">Click here to download</a>

用“some_name”作为文件名下载:

<a href="file link" download="some_name" target="_blank">Click here to download</a>

添加target="_blank",我们将使用一个新的标签,而不是实际的标签,而且它将有助于在某些情况下下载属性的正确行为。

它遵循与同源策略相同的规则。您可以在MDN Web Doc同源策略页面上了解有关此策略的更多信息

你可以在MDN Web Doc锚的属性页面上了解更多关于这个下载HTML5属性的信息。

元标签并不是实现这一结果的可靠方法。一般来说,你甚至不应该这样做——应该由用户/用户代理来决定如何处理你提供的内容。如果用户愿意,总是可以强制浏览器下载文件。

如果您仍然希望强制浏览器下载文件,请直接修改HTTP报头。下面是一个PHP代码示例:

$path = "path/to/file.pdf";
$filename = "file.pdf";
header('Content-Transfer-Encoding: binary');  // For Gecko browsers mainly
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($path)) . ' GMT');
header('Accept-Ranges: bytes');  // Allow support for download resume
header('Content-Length: ' . filesize($path));  // File size
header('Content-Encoding: none');
header('Content-Type: application/pdf');  // Change the mime type if the file is not PDF
header('Content-Disposition: attachment; filename=' . $filename);  // Make the browser display the Save As dialog
readfile($path);  // This is necessary in order to get it to actually download the file, otherwise it will be 0Kb

请注意,这只是对HTTP协议的扩展;有些浏览器可能会忽略它。

我遇到了同样的问题,找到了一个迄今为止非常有效的解决方案。你把下面的代码放在你的。htaccess文件中:

<FilesMatch "\.(?i:pdf)$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</FilesMatch>

它来自于强制文件下载而不是显示在浏览器中。

添加响应头Content-Disposition:附件;后面跟着文件名。删除Meta Content-Disposition;Inline;哪个会在同一个窗口中打开文档

在java中,它被设置为

response.setHeader("Content-Disposition", "attachment;filename=test.jpg");

服务器端解决方案更具兼容性,直到“download”属性在所有浏览器中实现。

一个Python示例可以是文件存储的自定义HTTP请求处理程序。指向文件存储的链接是这样生成的:

http://www.myfilestore.com/filestore/13/130787e71/download_as/desiredName.pdf

代码如下:

class HTTPFilestoreHandler(SimpleHTTPRequestHandler):

    def __init__(self, fs_path, *args):
        self.fs_path = fs_path                          # Filestore path
        SimpleHTTPRequestHandler.__init__(self, *args)

    def send_head(self):
        # Overwrite SimpleHTTPRequestHandler.send_head to force download name
        path = self.path
        get_index = (path == '/')
        self.log_message("path: %s" % path)
        if '/download_as/' in path:
            p_parts = path.split('/download_as/')
            assert len(p_parts) == 2, 'Bad download link:' + path
            path, download_as = p_parts
        path = self.translate_path(path )
        f = None
        if os.path.isdir(path):
            if not self.path.endswith('/'):
                # Redirect browser - doing basically what Apache does
                self.send_response(301)
                self.send_header("Location", self.path + "/")
                self.end_headers()
                return None
            else:
                return self.list_directory(path)
        ctype = self.guess_type(path)
        try:
            f = open(path, 'rb')
        except IOError:
            self.send_error(404, "File not found")
            return None
        self.send_response(200)
        self.send_header("Content-type", ctype)
        fs = os.fstat(f.fileno())
        self.send_header("Expires", '0')
        self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
        self.send_header("Cache-Control", 'must-revalidate, post-check=0, pre-check=0')
        self.send_header("Content-Transfer-Encoding", 'binary')
        if download_as:
            self.send_header("Content-Disposition", 'attachment; filename="%s"' % download_as)
        self.send_header("Content-Length", str(fs[6]))
        self.send_header("Connection", 'close')
        self.end_headers()
        return f


class HTTPFilestoreServer:

    def __init__(self, fs_path, server_address):
        def handler(*args):
            newHandler = HTTPFilestoreHandler(fs_path, *args)
            newHandler.protocol_version = "HTTP/1.0"
        self.server = BaseHTTPServer.HTTPServer(server_address, handler)

    def serve_forever(self, *args):
        self.server.serve_forever(*args)


def start_server(fs_path, ip_address, port):
    server_address = (ip_address, port)
    httpd = HTTPFilestoreServer(fs_path, server_address)

    sa = httpd.server.socket.getsockname()
    print "Serving HTTP on", sa[0], "port", sa[1], "..."
    httpd.serve_forever()