2

I want to send the user a file when he clicks the appropriate button and I'm suing the following.

# Prepare selected file for download...
send_file( '/home/nikos/wsgi/static/files/' + filename )

But no matter what file the usser selects iam always receiving this response.

[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)

How will I be able to send the selected file to the user?

1 Answer 1

1

Try this.. Before sending the file do,

file_name.encode('utf-8')

So pass these additional parameters to send_file(), I've got these from this thread

attachment_filename=file_name.encode('utf-8'),as_attachment=True, conditional=True

So your send_file might look like this...

filepath = '/home/nikos/wsgi/static/files/' + filename
filepath = filepath.encode('utf-8')

send_file(attachment_filename=filepath,as_attachment=True, conditional=True)

Let me know if it worked so I can try further.

Sign up to request clarification or add additional context in comments.

10 Comments

I'am consused. Could you please show me exactly how i should reform my code?
Post the name of the file once(filename). I think the filename is causing the issue.
send_file ( filepath, attachment_filename=filepath, as_attachment=True, conditional=True ) i get TypeError: cannot use a string pattern on a bytes-like object
The filename after checking ti it has the correct value. send_file ( attachment_filename=filepath, as_attachment=True, conditional=True ) outputs this error TypeError: send_file() missing 1 required positional argument: 'filename_or_fp
Try this line... send_file(filename.decode('utf-8'), attachment_filename=filepath, as_attachment=True ) This should probably fix your TypeError
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.