Django传文件(FILES用法)

Django的FILES方法可用于传各类文件,此处以传输图像为例。

客户端代码:

import requests
import json

data = {'text':'我们竟成路人了sd4ds2ad12sa1d2sa1'}
with open('b.png', 'rb') as f:
    datas = f.read()
    files = {'video_data':datas}
r = requests.post('http://127.0.0.1:8000/say/',data=data, files = files)
print(r)
print(json.loads(r.text))

 服务端代码

from django.http import JsonResponse
from django.views import View
import os

class updata(View):
    def post(self, requests):
        if requests.method == "POST":
            text = requests.POST.get('text')
            print('>>>', text)
            files = requests.FILES.get("video_data").chunks()
            with open('1.png','wb+') as f:
                for i in files:
                    f.write(i)
            return JsonResponse({'code':200, 'msg':'成功','data':''})
        else:
            return JsonResponse({'code':500, 'msg':'失败','data':''})

 

posted @ 2021-04-13 16:19  牛郎  阅读(909)  评论(0编辑  收藏  举报