我有一些大尺寸的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/


当前回答

从答案到强制浏览器保存文件为单击链接后:

<a href="path/to/file" download>Click here to download</a>

其他回答

如果你需要强制下载页面上的单个链接,一个非常简单的方法就是使用href-link中的HTML5 download-属性。

参见:http://davidwalsh.name/download-attribute

有了这个,你可以重命名用户将下载的文件,同时它强制下载。

这是否是一种好的做法一直存在争议,但在我的情况下,我有一个PDF文件的嵌入式查看器,查看器不提供下载链接,所以我必须单独提供一个。在这里,我希望确保用户不会在web浏览器中打开PDF文件,否则会令人困惑。

这将不需要打开另存为对话框,但将下载链接直接到预设的下载目的地。当然,如果你是为别人做一个网站,需要他们手动写属性到他们的链接可能是一个坏主意,但如果有办法让属性到链接,这可能是一个简单的解决方案。

在HTML代码中的文件名之后,我添加?forcedownload=1

对我来说,这是触发对话框进行保存或下载的最简单方法。

我为Firefox找到了一个非常简单的解决方案(只适用于相对而不是直接href): add type="application/octet-stream":

<a href="./file.pdf" id='example' type="application/octet-stream">Example</a>

服务器端解决方案更具兼容性,直到“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()

如果你在浏览器中有一个插件知道如何打开PDF文件,它会直接打开。比如图像和HTML内容。

因此,另一种方法是在响应中不发送MIME类型。这样,浏览器就永远不会知道哪个插件应该打开它。因此,它会给你一个保存/打开对话框。