やりたいこと
Jinja2のテンプレートを使用して、HTMLにファイルのパスをaタグのhrefに埋め込みます。
そのリンクをBottle側で検知してstatic_file
でダウンロードします。
現状
マルチバイト文字を含まなければ正常にダウンロードできます。
マルチバイト文字を1文字でも含んでいると、クリックした時に真っ白な画面に飛ばされます。画面にはエラーも何も出ません。
(コンソールには出ます)
コード
python
1@route("/result/download/<dir_name:path>/<file_name:path>") 2def download(dir_name, file_name): 3 return static_file(dir_name+"/"+file_name, root="./out", download=True) 4
html
1<a href="./result/download/sjis/{{ sjisfile }}">DOWNLOAD SJIS</a>
エラー
Traceback (most recent call last): File "/home/yatahachi/.pyenv/versions/3.6.1/lib/python3.6/wsgiref/handlers.py", line 138, in run self.finish_response() File "/home/yatahachi/.pyenv/versions/3.6.1/lib/python3.6/wsgiref/handlers.py", line 180, in finish_response self.write(data) File "/home/yatahachi/.pyenv/versions/3.6.1/lib/python3.6/wsgiref/handlers.py", line 274, in write self.send_headers() File "/home/yatahachi/.pyenv/versions/3.6.1/lib/python3.6/wsgiref/handlers.py", line 333, in send_headers self._write(bytes(self.headers)) File "/home/yatahachi/.pyenv/versions/3.6.1/lib/python3.6/wsgiref/headers.py", line 142, in __bytes__ return str(self).encode('iso-8859-1') UnicodeEncodeError: 'latin-1' codec can't encode character '\uff51' in position 91: ordinal not in range(256) 127.0.0.1 - - [21/Jul/2017 05:41:33] "GET /result/download/sjis/[sjis]a%EF%BD%91[1-10].txt HTTP/1.1" 500 59 ---------------------------------------- Exception happened during processing of request from ('127.0.0.1', 44282) Traceback (most recent call last): File "/home/yatahachi/.pyenv/versions/3.6.1/lib/python3.6/wsgiref/handlers.py", line 138, in run self.finish_response() File "/home/yatahachi/.pyenv/versions/3.6.1/lib/python3.6/wsgiref/handlers.py", line 180, in finish_response self.write(data) File "/home/yatahachi/.pyenv/versions/3.6.1/lib/python3.6/wsgiref/handlers.py", line 274, in write self.send_headers() File "/home/yatahachi/.pyenv/versions/3.6.1/lib/python3.6/wsgiref/handlers.py", line 333, in send_headers self._write(bytes(self.headers)) File "/home/yatahachi/.pyenv/versions/3.6.1/lib/python3.6/wsgiref/headers.py", line 142, in __bytes__ return str(self).encode('iso-8859-1') UnicodeEncodeError: 'latin-1' codec can't encode character '\uff51' in position 91: ordinal not in range(256) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/yatahachi/.pyenv/versions/3.6.1/lib/python3.6/wsgiref/handlers.py", line 141, in run self.handle_error() File "/home/yatahachi/.pyenv/versions/3.6.1/lib/python3.6/wsgiref/handlers.py", line 368, in handle_error self.finish_response() File "/home/yatahachi/.pyenv/versions/3.6.1/lib/python3.6/wsgiref/handlers.py", line 180, in finish_response self.write(data) File "/home/yatahachi/.pyenv/versions/3.6.1/lib/python3.6/wsgiref/handlers.py", line 274, in write self.send_headers() File "/home/yatahachi/.pyenv/versions/3.6.1/lib/python3.6/wsgiref/handlers.py", line 331, in send_headers if not self.origin_server or self.client_is_modern(): File "/home/yatahachi/.pyenv/versions/3.6.1/lib/python3.6/wsgiref/handlers.py", line 344, in client_is_modern return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9' TypeError: 'NoneType' object is not subscriptable During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/yatahachi/.pyenv/versions/3.6.1/lib/python3.6/socketserver.py", line 317, in _handle_request_noblock self.process_request(request, client_address) File "/home/yatahachi/.pyenv/versions/3.6.1/lib/python3.6/socketserver.py", line 348, in process_request self.finish_request(request, client_address) File "/home/yatahachi/.pyenv/versions/3.6.1/lib/python3.6/socketserver.py", line 361, in finish_request self.RequestHandlerClass(request, client_address, self) File "/home/yatahachi/.pyenv/versions/3.6.1/lib/python3.6/socketserver.py", line 696, in __init__ self.handle() File "/home/yatahachi/.pyenv/versions/3.6.1/lib/python3.6/wsgiref/simple_server.py", line 133, in handle handler.run(self.server.get_app()) File "/home/yatahachi/.pyenv/versions/3.6.1/lib/python3.6/wsgiref/handlers.py", line 144, in run self.close() File "/home/yatahachi/.pyenv/versions/3.6.1/lib/python3.6/wsgiref/simple_server.py", line 35, in close self.status.split(' ',1)[0], self.bytes_sent AttributeError: 'NoneType' object has no attribute 'split' ----------------------------------------
ダウンロードリンクをクリックした時のエラーです。
いろいろエラーが出ますが、解決策がわかりません。
ファイル名はどうしても日本語を含めなくてはいけません。
解決策がありましたら、どうかご教示ください。お願いいたします。
ログには「 /result/download/sjis/[sjis]a%EF%BD%91[1-10].txt 」とありますが、これは本当にそのページのリンク <a href="./result/download/utf_8/{{ utf8file }}">DOWNLOAD UTF8</a> から遷移したときのものでしょうか?このURL生成でsjisというログがでるのが不思議です。
回答ありがとうございます!おっしゃる通りです。HTMLのリンクはutf8とsjisの二つ用意しており、間違えてutf8の方を記載してしまっていました。修正いたしました。
static_fileメソッドのdownload引数を指定しないようにしたら、自動的にダウンロードはしてくれなくなりましたが一応読み込めるようになりました。これしか方法はないのでしょうか…
回答1件
あなたの回答
tips
プレビュー