使用web.py创建下载命令
我刚开始学习web.py,想要创建一个下载命令。
我创建了这个:
import web
urls = (
'/', 'index',
'/download', 'Download'
)
class index:
def GET(self):
return "Hello, world!"
class Download:
def GET(self):
path = 'http:\\localhost:8080\C:\11\229077_6482396906_558_n.jpg'
web.header('Content-Disposition', 'attachment; filename="fname.ext"')
web.header('Content-type','images/jpeg')
web.header('Content-transfer-encoding','binary')
return open(path, 'rb').read()
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
但是我遇到了两个主要问题:
当我访问
http://localhost:8080/download
时,出现了500内部服务器错误。这是为什么呢?我不能选择想要下载的文件(只能手动更改路径参数)。我该如何给这个函数传递外部参数呢?
2 个回答
0
2. 比如把这个加到你的代码里:
filename = web.input().file
这样,当你输入这个链接时,它就会返回你想要的文件名:
yoursite.tld/downloads?file=FILENAME
想了解更多相关内容,可以点击这里: http://webpy.org/cookbook/input