Getting error while storing the file into folder using Django and Python -


i getting error while storing file folder using django , python. explaining 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 character buffer object [12/sep/2017 10:52:35] "post /downloadfile/ http/1.1" 500 71558 performing system checks... 

i explaining code below.

def downloadfile(request):     """ function helps download file 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         open(settings.file_path + filename, 'w') fyl:             fyl.write(urllib2.urlopen(url).read())             fyl.write(response)         return response 

i getting error in fyl.write(response) line. here including remote file , download it. after downloading storing inside folder. please me resolve error.

import os django.conf import settings django.http import httpresponse  def download(request, path):     if request.method == 'post':        url = request.post.get('file')        path_with_filenmae = "status.txt" #you can define file name path        file_path = os.path.join(settings.media_root, path_with_filenmae)        if os.path.exists(file_path):            open(file_path, 'rb') fh:            response = httpresponse(fh.read(), content_type="text/plain")            response['content-disposition'] = 'inline; filename=' + os.path.basename(file_path)            return response        raise http404 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -