In my Python code, I received a file as InMemoryUploadedFile
and then I'm trying to save that file on disk. It says that file stored successfully but when I try to open that file, I get this error:
Here is the code snippet that I'm trying with
with Path("check.pdf").open(mode="wb") as output_file:
output_file.write(fileToParse.read())
Another code that I tried before is this one:
outfd, filePath = tempfile.mkstemp(suffix='check.pdf', dir=os.getcwd())
with open(filePath, "wb") as dest:
dest.write(fileToParse)
os.close(outfd)
In both cases, I get same error. I've checked different posts and even on here SO, first solution is working for many people. But I don't know why it is not storing proper file for me.
The type of file fileToParse
that I received is <class 'django.core.files.uploadedfile.InMemoryUploadedFile'>
and fileToParse.read()
returns <class 'bytes'>
Can anyone tell me what am I doing wrong here?