如何让本地服务器上的文件通过HTTP可下载?
框架:Django
语言:Python
操作系统:Ubuntu
举个例子,假设我在 "/home/username/project/" 这个文件夹里有一个文件 "xyz.pdf"。我有一个网页,上面有一个下载按钮。这样,如果有人点击这个下载按钮,就应该能下载到这个文件 xyz.pdf。
我做了以下几件事:
- 创建了一个网页,上面有一个下载按钮,链接设置为 "download/"(大家都知道这个网址其实没什么特别的)
- 这个链接会被转到 urls.py 文件,然后找到对应的视图,比如说 "xyzdownload"
- 在 xyzdownload 这个视图里,我写了以下代码:
response = urllib2.urlopen('./project/xyz.pdf')
html = response.read()
我遇到的错误是:未知的 URL 类型:./project/xyz.pdf
如果大家需要更多的解释,请告诉我。非常感谢!
3 个回答
你可以试试使用 mechanize 这个工具,它可以帮助你点击文本和通过正则表达式跟踪网址。
import mechanize
br = mechanize.Browser()
# Browser options
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
# Want debugging messages?
#br.set_debug_http(True)
#br.set_debug_redirects(True)
#br.set_debug_responses(True)
# User-Agent (this is cheating, ok?)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
# Download xyz.pdf ;)
f = br.retrieve('http://www.site.com/xyz.pdf')[0]
print f
fh = open(f)
# or you may try with click download just uncomment
# br.click_link(text_regex="download")
# or by url regex
# br.click_link(url_regex="project/xyz.pdf", nr=0)
还有很多资源和教程可以查看,点击这里 http://wwwsearch.sourceforge.net/mechanize/。
我遇到的错误是未知的 URL 类型:./project/xyz.pdf
这很简单:./project/xyz.pdf 不是 一个 URL。维基百科上说:
每个 URL 都由以下一些部分组成:方案名称(通常叫做协议),后面跟着一个冒号,然后根据方案,可能会有一个主机名(或者说 IP 地址)、一个端口号、要获取的资源路径或要运行的程序,接着,对于像公共网关接口(CGI)脚本这样的程序,可能还有一个查询字符串,对于 HTML 文档,还有一个可选的锚点,用于指定页面应该从哪里开始显示。
你没有提供 方案名称 和 主机名。不过,我不明白你为什么这样做。调用 urlopen 的意思是:在 我的 电脑上下载这个文件。这 并不 是“把文件给用户”。
顺便提一下,xyzdownload 视图应该按照 @Ryan Ginstrom 建议的那样来定义。当然,你需要把代码调整为适应 Django。
一般来说,你只需要把文件放在像apache这样的服务器上,然后提供一个链接让别人访问。如果这个文件是动态生成的,或者你想要限制它的访问,这里有一个使用cherrypy的方法:
@cherrypy.expose
def download(self, filename):
"""
Download the specified XML file
"""
# only if it's one of these files
if filename not in "foo.xml bar.xml".split():
return "Nice try"
content = open("/path/to/media/" + filename).read()
cherrypy.response.headers['Content-Type'] = 'application/xml'
cherrypy.response.headers['Content-Length'] = len(content)
disp = 'attachment; filename=%s' filename
cherrypy.response.headers['Content-Disposition'] = disp
cherrypy.response.headers['filename'] = 'application/xml'
cherrypy.response.headers['pragma'] = ""
cherrypy.response.headers['content-cache'] = ""
return content
链接的格式大概是这样的:
http://example.com/download/foo.xml