How to get binary data in Django!

Forgive me if this is a little newbie question, I started learning Django yesterday, and I try not to get into bad habits, that is, I try to do the “django path” from the very beginning.

I have a view that receives binary data as a post http field. Now, Django, of course, will auto-convert my binary data to a unicode string.

My question is: how can I get raw binary data?

A few things happened to me. Let requestis the request that I am processing.

  • Usage request.raw_post_datawill include parsing the data again - when it actually request.POSTactually stores the raw data, and I'm actually just trying to get around the conversion on the fly (and also, this is new in the development version).
  • Using base64 or so to transfer data will work, but the overhead seems too big when data transfer itself is not a problem.
  • Executing request.encoding="foo"before getting this field (and reassigning subsequently) does not work either because I still get the Unicode string, but I feel a bit dirty hacking. Using "base64"here (not as bad as for transfer coding) gives me AssertionError.

Thanks for your ideas!

EDIT: - , , POST. , , , - script. POST , .

+3
1

, :)

Python .

binascii - ASCI


:

( POST)

def handleFile(self, request):
    file = request.FILES["file"]
    destination = open('filename.ext', 'wb')
        for chunk in file.chunks():
            destination.write(chunk)
    destination.close()

.

+4

Source: https://habr.com/ru/post/1781431/


All Articles