import os
from django.utils import simplejson as json


class qqFileUploader(object):

    def __init__(self, allowedExtensions=None, sizeLimit=1024):
        self.allowedExtensions = allowedExtensions or []
        self.sizeLimit = sizeLimit

    def handleUpload(self, request, uploadDirectory):
        #read file info from stream
        uploaded = request.read
        #get file size
        fileSize = int(uploaded.im_self.META["CONTENT_LENGTH"])
        #get file name
        fileName = uploaded.im_self.META["HTTP_X_FILE_NAME"]
        #check first for allowed file extensions
        if self._getExtensionFromFileName(fileName) in self.allowedExtensions or ".*" in self.allowedExtensions:
            #check file size
            if fileSize <= self.sizeLimit:
                #upload file
                #write file
                file = open(os.path.join(uploadDirectory, fileName), "wb+")
                file.write(request.read(fileSize))
                file.close()
                return json.dumps({"success": True})
            else:
                return json.dumps({"error": "File is too large."})
        else:
            return json.dumps({"error": "File has an invalid extension."})

    def _getExtensionFromFileName(self, fileName):
        filename, extension = os.path.splitext(fileName)
        return extension

import os
from django.utils import simplejson as json


class qqFileUploader(object):

    def __init__(self, allowedExtensions=None, sizeLimit=1024):
        self.allowedExtensions = allowedExtensions or []
        self.sizeLimit = sizeLimit

    def handleUpload(self, request, uploadDirectory):
        #read file info from stream
        uploaded = request.read
        #get file size
        fileSize = int(uploaded.im_self.META["CONTENT_LENGTH"])
        #get file name
        fileName = uploaded.im_self.META["HTTP_X_FILE_NAME"]
        #check first for allowed file extensions
        #read the file content, if it is not read when the request is multi part then the client get an error
        fileContent = uploaded(fileSize)
        if self._getExtensionFromFileName(fileName) in self.allowedExtensions or ".*" in self.allowedExtensions:
            #check file size
            if fileSize <= self.sizeLimit:
                #upload file
                #write file
                file = open(os.path.join(uploadDirectory, fileName), "wb+")
                file.write(fileContent)
                file.close()
                return json.dumps({"success": True})
            else:
                return json.dumps({"error": "File is too large."})
        else:
            return json.dumps({"error": "File has an invalid extension."})

    def _getExtensionFromFileName(self, fileName):
        filename, extension = os.path.splitext(fileName)
        return extension

Read the file content, if it is not read when the request is multipart then the client get an error
Read the file content, if it is not read when the request is multipart then the client get an error

  import os
  from django.utils import simplejson as json
  
  
  class qqFileUploader(object):
  
      def __init__(self, allowedExtensions=None, sizeLimit=1024):
          self.allowedExtensions = allowedExtensions or []
          self.sizeLimit = sizeLimit
  
      def handleUpload(self, request, uploadDirectory):
          #read file info from stream
          uploaded = request.read
          #get file size
          fileSize = int(uploaded.im_self.META["CONTENT_LENGTH"])
          #get file name
          fileName = uploaded.im_self.META["HTTP_X_FILE_NAME"]
          #check first for allowed file extensions
+         #read the file content, if it is not read when the request is multi part then the client get an error
+         fileContent = uploaded(fileSize)
          if self._getExtensionFromFileName(fileName) in self.allowedExtensions or ".*" in self.allowedExtensions:
              #check file size
              if fileSize <= self.sizeLimit:
                  #upload file
                  #write file
                  file = open(os.path.join(uploadDirectory, fileName), "wb+")
-                 file.write(request.read(fileSize))
+                 file.write(fileContent)
                  file.close()
                  return json.dumps({"success": True})
              else:
                  return json.dumps({"error": "File is too large."})
          else:
              return json.dumps({"error": "File has an invalid extension."})
  
      def _getExtensionFromFileName(self, fileName):
          filename, extension = os.path.splitext(fileName)
          return extension
  
Read the file content, if it is not read when the request is multipart then the client get an error
## Code Before:
import os
from django.utils import simplejson as json


class qqFileUploader(object):

    def __init__(self, allowedExtensions=None, sizeLimit=1024):
        self.allowedExtensions = allowedExtensions or []
        self.sizeLimit = sizeLimit

    def handleUpload(self, request, uploadDirectory):
        #read file info from stream
        uploaded = request.read
        #get file size
        fileSize = int(uploaded.im_self.META["CONTENT_LENGTH"])
        #get file name
        fileName = uploaded.im_self.META["HTTP_X_FILE_NAME"]
        #check first for allowed file extensions
        if self._getExtensionFromFileName(fileName) in self.allowedExtensions or ".*" in self.allowedExtensions:
            #check file size
            if fileSize <= self.sizeLimit:
                #upload file
                #write file
                file = open(os.path.join(uploadDirectory, fileName), "wb+")
                file.write(request.read(fileSize))
                file.close()
                return json.dumps({"success": True})
            else:
                return json.dumps({"error": "File is too large."})
        else:
            return json.dumps({"error": "File has an invalid extension."})

    def _getExtensionFromFileName(self, fileName):
        filename, extension = os.path.splitext(fileName)
        return extension

## Instruction:
Read the file content, if it is not read when the request is multipart then the client get an error
## Code After:
import os
from django.utils import simplejson as json


class qqFileUploader(object):

    def __init__(self, allowedExtensions=None, sizeLimit=1024):
        self.allowedExtensions = allowedExtensions or []
        self.sizeLimit = sizeLimit

    def handleUpload(self, request, uploadDirectory):
        #read file info from stream
        uploaded = request.read
        #get file size
        fileSize = int(uploaded.im_self.META["CONTENT_LENGTH"])
        #get file name
        fileName = uploaded.im_self.META["HTTP_X_FILE_NAME"]
        #check first for allowed file extensions
        #read the file content, if it is not read when the request is multi part then the client get an error
        fileContent = uploaded(fileSize)
        if self._getExtensionFromFileName(fileName) in self.allowedExtensions or ".*" in self.allowedExtensions:
            #check file size
            if fileSize <= self.sizeLimit:
                #upload file
                #write file
                file = open(os.path.join(uploadDirectory, fileName), "wb+")
                file.write(fileContent)
                file.close()
                return json.dumps({"success": True})
            else:
                return json.dumps({"error": "File is too large."})
        else:
            return json.dumps({"error": "File has an invalid extension."})

    def _getExtensionFromFileName(self, fileName):
        filename, extension = os.path.splitext(fileName)
        return extension
