Why does request.FILES[“file”].open() is None, though request.FILES.[“file”].name works well?

后端 未结 1 1232
既然无缘
既然无缘 2021-01-28 00:09

I\'m trying to get image file using ajax into Django views.py. I can print the file name and size of it. But when I open the file, it become None..!

The reason I\'m try

相关标签:
1条回答
  • 2021-01-28 00:31

    Once you have called read() on a file subsequent calls will not yield any data. The first call to read() reads the entire buffer and there will not be anything left when you call it again

        print(type(request.FILES["file"].read())) # This line has read the entire buffer
        imageFile = request.FILES["file"]
        receipt_image = imageFile.read() # There is nothing left to read so this returns b''
    
    0 讨论(0)
提交回复
热议问题