我正在使用此视图功能来提供下载文件,
from django.http import FileResponse
def sendFiles(request):
filename = request.GET['filename']
pathToFile = os.path.join(filesDir, filename)
response = FileResponse(open(pathToFile, 'rb'))
response['Content-Type'] = 'application/octet-stream'
response[
'Content-Disposition'] = 'attachment; filename="{}"'.format(filename)
response['Content-Length'] = os.path.getsize(pathToFile)
# HttpResponse(open(os.path.join(os.getcwd(), 'LYYDownloaderServer.log'), 'r'), content_type='text/plain')
return response
将http://127.0.0.1:8000/VideoParser/files/?filename=Git-2.8.4-32-bit.exe
粘贴到浏览器地址栏并输入后,然后文件开始下载,当我暂停下载,然后服务器控制台输出以下异常,任何人都可以在这里弄清楚什么是错的?
使用Python 3.5.1和Django 1.9在Win10上测试
Exception happened during processing of request from ('127.0.0.1', 54276)
Traceback (most recent call last):
File "C:\Users\iMath\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 138, in run
self.finish_response()
File "C:\Users\iMath\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 180, in finish_response
self.write(data)
File "C:\Users\iMath\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 279, in write
self._write(data)
File "C:\Users\iMath\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 453, in _write
self.stdout.write(data)
File "C:\Users\iMath\AppData\Local\Programs\Python\Python35-32\lib\socket.py", line 593, in write
return self._sock.send(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\iMath\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 141, in run
self.handle_error()
File "C:\Users\iMath\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\servers\basehttp.py", line 92, in handle_error
super(ServerHandler, self).handle_error()
File "C:\Users\iMath\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 368, in handle_error
self.finish_response()
File "C:\Users\iMath\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 180, in finish_response
self.write(data)
File "C:\Users\iMath\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 274, in write
self.send_headers()
File "C:\Users\iMath\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 331, in send_headers
if not self.origin_server or self.client_is_modern():
File "C:\Users\iMath\AppData\Local\Programs\Python\Python35-32\lib\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 "C:\Users\iMath\AppData\Local\Programs\Python\Python35-32\lib\socketserver.py", line 628, in process_request_thread
self.finish_request(request, client_address)
File "C:\Users\iMath\AppData\Local\Programs\Python\Python35-32\lib\socketserver.py", line 357, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Users\iMath\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\servers\basehttp.py", line 99, in __init__
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
File "C:\Users\iMath\AppData\Local\Programs\Python\Python35-32\lib\socketserver.py", line 684, in __init__
self.handle()
File "C:\Users\iMath\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\servers\basehttp.py", line 179, in handle
handler.run(self.server.get_app())
File "C:\Users\iMath\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\handlers.py", line 144, in run
self.close()
File "C:\Users\iMath\AppData\Local\Programs\Python\Python35-32\lib\wsgiref\simple_server.py", line 35, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'