繁体   English   中英

从本地文件夹下载文件

[英]Download file from local folder

我正在尝试此代码

@app.route('/process/<user_id>/<file_format>/<download>', methods=['POST', 'GET'])
def download(user_id, file_format, download):
    if request.method == 'GET':

        response = urllib2.urlopen("http://"+socket.gethostname()+"app/documents/"+ download)
        html = response.read()
        return html

但我得到:

URLError: <urlopen error [Errno -2] Name or service not known>

如果我只response = urllib2.urlopen("app/documents/"+ download)

我得到:

ValueError: unknown url type: app/documents/thereport_1712818a-39a3-436e-985c-84f1e8d43346.pdf

因此,基本上我该如何从文档文件夹中获取文件?

根据您的代码,我知道您想下载位于您自己计算机上的html文件。 第一:

response = urllib2.urlopen("http://"+socket.gethostname()+"app/documents/"+ download)

应该

response = urllib2.urlopen("http://"+socket.gethostname()+"/app/documents/"+ download)

除非“ app”是您的主机名的一部分(我认为不是)。

其次...确保使用wget可以执行相同的操作(这意味着您在本地安装了http服务器,该服务器侦听端口80并可以提供请求的文件):

wget <URL>
from flask import Flask, redirect
app = Flask(__name__)

@app.route('/process/<user_id>/<file_format>/<download>', methods=['POST', 'GET'])
def download(user_id, file_format, download):
    if request.method == 'GET':
        return redirect("http://{0}/app/documents/{1}".format(request.host,download))
@app.route('/process/<user_id>/<file_format>/<download>')
def download(user_id, file_format, download):
    return redirect(url_for('static', filename='documents/'+download))

这就是我想要的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM