Generate a link with utf-8 values and passing it to a Flask route [closed]
Asked Answered
T

1

0

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 }}">

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 )

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

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.

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:

http://superhost.gr/files/download?filename=%CE%94%CE%B7%CE%BC%CE%B9%CE%BF%CF%85%CF%81%CE%B3%CE%AF%CE%B1%20Win10%20Bootable%20Flash%20Disks.txt

Turaco answered 30/9, 2018 at 10:12 Comment(14)
I can't understand, You want to get value of {{ filename }}?Shirtmaker
I want to pass that value to the link with unicode encoding becasue it cotnains Greek letters.Yoo
I wrote code but i don't know why doesn't work. Check jsfiddle.net/skv89cnz mayby help youShirtmaker
i see here that you pass tha value Νικόλαος το the parameter. In my case the value is being retrived from Bottle framework with the correct encoding but unfortunately when <meta http-equiv="REFRESH" content="5; URL=http://superhost.gr/files/download?file={{ filename }}"> forms it doesnt redirect to the url with utf-8 encoding. Hoe cna i ensure that?Yoo
You can use the code in your framework and change filename in it or watring for set value in {{ filename }} then running codeShirtmaker
You want to prevent from utf-8 encoding?Shirtmaker
I want to pass the value of ?file={{ filename }} to the link as utf-8Yoo
In provided jsfiddle, it return url in utf8Shirtmaker
When the generated link is sent back to the flask route function send_from_directory( filepath, filename, as_attachment=True ) cannot download the file because its filename contains non latin-characters, which means that the url is not utf-8 formatted.Yoo
Anybody else have an idea that can help here?!Yoo
Hey, I can help. Can you post the entire Flask route, entire Jinja2 template, and exactly what you're trying to do that doesn't work?Turrell
Hello, i'am describing in my question that when i try to generate a valid link with filename query variable and then use that link to load a flask route the filename returned from the link its NOT formed in`utf8' therefore iam getting an error. I'm displaying my code in my question, what else do you want me to show you?Yoo
As shown in the answer below, this already works just fine. If you are encountering problems you should share with us what you see instead. What output is produced (as bytes) for what sample inputs? What exactly happens, where, when you try this? What does the browser send (look at the browser developer tools, see the network tab, retrieve URLs there). Do you have server logs, etc.?Witchy
Why my question is closed? I just noticed it.Yoo
P
1

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

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>
Pyuria answered 25/10, 2018 at 20:52 Comment(5)
In your example you are using Flask's embedded web server, while in my example i'am using Apache web server, which in my case returns the encoding error. Could you please try to use Apache and see if it works with you or if it returnds enocding error or bad request ?Yoo
If it's an Apache config problem, see #914369 may be helpLevanter
@ΝικόλαοςΒέργος: please add those details to your question. This sounds like a Apache / WSGI setup problem, not a Flask problem.Witchy
@Shine Zhang: I have looked this link and tested by adding AddDefaultCharset utf-8 into httpd.conf but the issue remains.Yoo
@Martijn Pieters: I tend to think so too, that perhaps is an Apache/WSGI issue but i don't know what to do to solve it. I added these details to my original questions as you suggetsed.Yoo

© 2022 - 2024 — McMap. All rights reserved.