我目前正在建立一个网站,从中获取用户上传的文件,对其进行一些处理,并提供一个链接供用户下载处理后的文件。我目前想在本地系统上提供文件的路径,我是 web2py 的新手,并且在执行此操作时遇到了麻烦。
有人可以帮我这样做吗?
问候
请参阅此链接以获取一些提示:webpy:如何流式传输文件,并且可能会添加一些如下代码:
BUF_SIZE = 262144
class download:
def GET(self):
file_name = # get from url
file_path = os.path.join('/path to your file', file_name)
f = None
try:
f = open(file_path, "rb")
webpy.header('Content-Type','application/octet-stream')
webpy.header('Content-disposition', 'attachment; filename=%s' % file_name)
while True:
c = f.read(BUF_SIZE)
if c:
yield c
else:
break
except Exception, e:
# throw 403 or 500 or just leave it
pass
finally:
if f:
f.close()