我想在用户单击相应按钮时向他发送一个文件,我正在起诉以下内容。
# Prepare selected file for download...
send_file( '/home/nikos/wsgi/static/files/' + filename )
但无论用户选择什么文件,我总是会收到此响应。
[Wed Sep 12 14:10:48.450211 2018] [wsgi:error] [pid 5172] [remote 46.103.174.201:14089] File "/home/nikos/wsgi/downloads.py", line 182, in file
[Wed Sep 12 14:10:48.450214 2018] [wsgi:error] [pid 5172] [remote 46.103.174.201:14089] send_file( '/home/nikos/wsgi/static/files/' + filename )
[Wed Sep 12 14:10:48.450219 2018] [wsgi:error] [pid 5172] [remote 46.103.174.201:14089] File "/usr/lib/python3.6/site-packages/flask/helpers.py", line 592, in send_file
[Wed Sep 12 14:10:48.450221 2018] [wsgi:error] [pid 5172] [remote 46.103.174.201:14089] file = open(filename, 'rb')
[Wed Sep 12 14:10:48.450237 2018] [wsgi:error] [pid 5172] [remote 46.103.174.201:14089] UnicodeEncodeError: 'ascii' codec can't encode characters in position 30-39: ordinal not in range(128)
我如何才能将选定的文件发送给用户?
请您参考如下方法:
试试这个.. 在发送文件之前,
file_name.encode('utf-8')
所以将这些附加参数传递给 send_file(),我从这个 thread 得到了这些参数
attachment_filename=file_name.encode('utf-8'),as_attachment=True, conditional=True
所以你的 send_file 可能看起来像这样......
filepath = '/home/nikos/wsgi/static/files/' + filename
filepath = filepath.encode('utf-8')
send_file(attachment_filename=filepath,as_attachment=True, conditional=True)
让我知道它是否有效,以便我可以进一步尝试。
