简体   繁体   中英

How to recieve formdata in django backend

I want to send the recorded wecam video to django backend, But can't figure out how to generate the video in backend.

function sendVideoToAPI () {
    
const url = '/upload/' 
let blob = new Blob(chunks, {type: media.type });

let fd = new FormData();
let file = new File([blob], 'recording');

fd.append('data', file);

fetch(url1, {
  method: 'POST',
  body: fd
  })
.then(res => console.log(res))
.catch(err => console.log(err))
}

In django views how to generate the video.Is there any way to do it..?

EDIT

Views.py

def upload(request):
    if request.method == 'POST':
       
        request.FILES.get('data') as f:
            print(f.size)
            print(type(f))

        return redirect("/")

while running server it gives The following error

Error

 request.FILES.get('data') as f:
                               ^
SyntaxError: invalid syntax

in django's view you can access to your files through request like this:

request.FILES.get('data') as f:
    # your code

NOTE : this will make you a 'TemporaryUploadedFile' which means that after working with that, it destroys automatically

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM