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

1 Answers1

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>
Kamil Niski
  • 4,580
  • 1
  • 11
  • 24
  • 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` ? – Νικόλαος Βέργος Oct 26 '18 at 08:18
  • If it's an Apache config problem, see https://stackoverflow.com/questions/913869/how-to-change-the-default-encoding-to-utf-8-for-apache may be help – Shihe Zhang Oct 27 '18 at 09:08
  • @ΝικόλαοςΒέργος: please **add those details to your question**. This sounds like a Apache / WSGI setup problem, not a Flask problem. – Martijn Pieters Oct 27 '18 at 15:14
  • @Shine Zhang: I have looked this link and tested by adding `AddDefaultCharset utf-8` into httpd.conf but the issue remains. – Νικόλαος Βέργος Oct 27 '18 at 16:01
  • @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. – Νικόλαος Βέργος Oct 27 '18 at 16:02