I am getting error while storing the file into folder using Django and Python. I am providing the error below.
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/opt/lampp/htdocs/rework/Nuclear/RFI15/vlnerable/plant/views.py", line 267, in downloadfile
fyl.write(response)
TypeError: expected a character buffer object
[12/Sep/2017 10:52:35] "POST /downloadfile/ HTTP/1.1" 500 71558
Performing system checks...
I am providing my code below.
def downloadfile(request):
""" This function helps to download the file from remote site"""
if request.method == 'POST':
URL = request.POST.get('file')
filename = "status.txt"
response = HttpResponse(content_type='text/plain')
response['Content-Disposition'] = 'attachment; filename='+filename
with open(settings.FILE_PATH + filename, 'w') as fyl:
fyl.write(urllib2.urlopen(URL).read())
fyl.write(response)
return response
I am getting that error in this fyl.write(response)
line. Here I am including the remote file and download it. After downloading its storing inside the folder.