

How to check the file type of python InMemoryUploadedFile file?
I am creating an app using python Django to merge documents and I would want to check the file type of the uploaded files because the code can only merge pdf file. I have tried to use endswith and split function to check the file type but there was error saying InMemoryUploadedFile has no such attributes. Hence, are there any other methods to check for file type of InMemoryUploadedFile file ? My code is shown below: I want to make sure that check that if the uploaded files are not pdf files, the code will ignore it (adding if statement & continue)
def mergedocument(request):
doc = request.FILES.getlist("file[]")
mergedoc =[]
for docu in doc:
fs = FileSystemStorage()
file_path = fs.save(docu.name, docu)
pdoc = documentsfile(docs=file_path)
pdoc.save()
mergedoc.append(docu)
merger = PdfFileMerger()
for pdf in mergedoc:
merger.append(pdf)
merger.write("C:\\Users\\Downloads\\Merged.pdf")
merger.close()
messages.info(request,a)
messages.info(request,'Files merged and stored in your Downloads folder')
return redirect('home')
Python


Use forms in Django and use the FIleExtensionValidator - https://docs.djangoproject.com/en/dev/ref/validators/#fileextensionvalidator
Login to add comment