Django: how save bytes object to models.FileField?


My web application has the following structure:

<ol><li>backend with Django</li> <li>frontend with React.</li> </ol>

I have a form with React. I send a file from client form and I receive the file in my Django application with an APIView.

I receive a m3u file as bytes object.

b'------WebKitFormBoundaryIaAPDyj9Qrx8DrWA\r\nContent-Disposition: form-data; name="upload"; filename="test.m3u"\r\nContent-Type: audio/x- mpegurl\r\n\r\n#EXTM3U\n#EXTINF:-1 tvg-ID="" tvg-name="...

I would save the file in a Django model to a models.FileField and convert bytes object to m3u file. How you do it?


You can try:

from django.core.files.base import ContentFile file_data = ContentFile(base64.b64decode(fileData)) object.file.save(file_name, file_data)

You can use your file_name with an .m3u extension, and you shall have it.


I solved using temporary file. I used this code:

extM3u = str.encode(body.decode('utf8').split('EXTM3U\n#')[1].split('------WebKitFormBoundary')[0]) fileTemp = NamedTemporaryFile(delete=True, dir='media/tmp') fileTemp.write(extM3u) filenameRe = re.compile('.*?filename=[\'"](.*?)[\'"]') filename = regParse(filenameRe, body.decode('utf8')) file = File(fileTemp, name=filename) m3u = M3u(titleField=filename, fileField=file) m3u.save()



  • Attempt to display prompt until correct entry is entered in using Javascript
  • Merging html files using XSLT?
  • Yii2 login give access to backend when user login is from frontend
  • Visual Studio Web Deployment - update publish profile to “Precompile during publishing”
  • How to share functions between components in angular?
  • How can I find the difference between two times [duplicate]
  • Arc gradients in Flutter?
  • Thick underline when hover AND when active
  • Sum children values and save result to parent in n-ary tree in Javascript
  • ROR + MVC Disable Browser Cache
  • Get the UTM tags with Facebook Marketing API
  • jQuery YQL SELECT FROM rss variable
  • Do iTunesConnect external testers get updates for *every* uploaded ipa file after Beta Approval?
  • can you use embedded ruby in custom javascript files in rails?
  • Jenkins sending notifications to the wrong commit id
  • Could not resolve all files for configuration ':react-native-vector-icons:classpath'
  • Videos won't upload
  • Write to elasticsearch from spark is very slow
  • How to put an object in the air?
  • How do I set the logging properties in a spring java configuration?
  • Auto send email based on the time and email address in database
  • Defer unused CSS
  • Unity Resources.load() won't work with external dll
  • Getting Microsoft Calibri font on Amazon EC2 ubuntu
  • content must have a ListView whose id attribute is 'android.R.id.list'
  • opencv deskewing a contour
  • Add checkbox dynamically using angular 2
  • Controller or RestController
  • how to specify different css for ie
  • Ajax call on Multiple selection in Select box
  • Calculate time from document
  • 'url' requires a non-empty first argument. The syntax changed in Django 1.5, see the docs
  • Amazon Elastick BeanStalk error: Failed to create the AWS Elastic Beanstalk application version
  • How to use Streams api peek() function and make it work?
  • `$http:badreq Bad Request Configuration` - from angular post method, what is wrong here?
  • How to decleare char *const argv[] in swift [duplicate]
  • Functions by reference or by variable, which to use when?
  • How to get rgb from transparent pixel in js
  • Angular FormGroup won't update it's value immediately after patchValue or setValue
  • WPF custom control and direct content support