Most of the time when we create a new text file with gedit in linux then the file is not saved with an extension of .txt
for text file.So how will I recognise it with django code because here I can't check file extension.Here is my code...
Let's say i have a resume field for each user in following models.py
class User(AbstractUser):
resume= models.FileField( upload_to=get_attachment_file_path,default=None, null=True,validators=[validate_file_extension])
Now i want to Validate the file for allowed extension so I made a validators.py as below
def validate_file_extension(fieldfile_obj):
megabyte_limit = 5.0 filesize = sys.getsizeof(fieldfile_obj) ext = os.path.splitext(fieldfile_obj.name)[1] print("extensionnnnnnnnnnnnn",ext) valid_extensions = ['.pdf', '.doc', '.docx', '.jpg', '.png', '.xlsx', '.xls','.txt','.odt'] if not ext.lower() in valid_extensions: raise ValidationError(u'Unsupported file extension.') elif filesize > megabyte_limit*1024*1024: raise ValidationError("Max file size is %s Byte" % str(megabyte_limit))
Now whenever I upload a text file in my api then it says unsupported file type because the code is unable to get the extension of linux text file.So how can i recognise that text file which is not saved as demo.txt
instead my text file is saved as only demo
but it is text file as seen from property of that file.
Also my next question is to get the size of each file uploaded in that FileField
.I am using PostgreSQL
as Dbms