生成具有utf-8值的链接,并将其传递到Flask路线 [英] Generate a link with utf-8 values and passing it to a Flask route

查看:51
本文介绍了生成具有utf-8值的链接,并将其传递到Flask路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何确保以下html url链接将以 utf-8 编码返回自身?

How can i ensure that the following html url link is going to return itself with utf-8 encoding?

<meta http-equiv="REFRESH" content="5; URL=http://superhost.gr/files/download?filename={{ filename }}">

现在,尽管从Flask中以 utf-8 检索文件名的值,但它也没有以 utf-8 的形式构成URL链接.

As it is now, although the value of filename is being retrieved from Flask as utf-8 it doesn't form the URL link also as utf-8.

这是我获取此值并尝试使用它来下载文件的方式.

Here is how i'm fetching this value and try to use it to download a file.

# Prepare selected file for download...
if request.args:
    filename = request.args.get('filename')         # value comes from template url link
    filepath = '/static/files/'
    return send_from_directory( filepath, filename, as_attachment=True )

我正在尝试在Apache/WSGI mod下生成与Jinja2/Flask的链接.

I'am trying to generate the link with Jinja2 / Flask under Apache/WSGI mod.

也许是mod_wsgi下的Apache导致了此问题?!

Perhaps Apache under mod_wsgi is causing this issue?!

我在浏览器中看到的错误是:

The error i'am seeing in the browser is:

Bad Request
The browser (or proxy) sent a request that this server could not understand.

根据Chrome的开发工具"/网络"标签为具有混合文件名(希腊语和英语)的测试文件生成的链接为:

The link that is generated according to Chrome's Developer Tool/Network Tab for a a test file with a mixed filename(greek + english) is:

推荐答案

我正在尝试重现您的问题,但我认为您应该提供更多信息.

I'm trying to reproduce your issue but I think that you should provide more information.

我尝试了以下设置,并且/redirect/正确返回了名为ΝικόλαοςΒέργος.pdf的文件.

I've tried the setup below and the file named Νικόλαος Βέργος.pdf is correctly returned by /redirect/.

app.py

from flask import render_template
from flask import Flask
from flask import request, send_from_directory
app = Flask(__name__)

@app.route('/')
def home():
    filename='Νικόλαος Βέργος.pdf'
    return render_template('home.html', filename=filename)

@app.route('/redirect/')
def redirect():
    if request.args:
        filename = request.args.get('filename')
        filepath = '/static/files/'
        return send_from_directory(filepath, filename, as_attachment=True)

templates/home.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta http-equiv="REFRESH" content="5; URL=http://127.0.0.1:5000/redirect/?filename={{ filename }}">
    <title>title</title>
  </head>
  <body>
  </body>
</html>

这篇关于生成具有utf-8值的链接,并将其传递到Flask路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆