[英]multiple file upload in django rest framework
就像這樣
import json
from rest_framework.decorators import api_view
from rest_framework.response import Response
@api_view(['POST'])
def foo(request):
attributes = json.loads(request.data['attributes']) # will be a 'dict'
xsd_file = request.data['xsd_file'] # "InMemoryUploadFile" instance
Iccs_file = request.data['Iccs_file'] # "InMemoryUploadFile" instance
return Response("some response")
我假設您正在編寫基於類的視圖。 這是你的view.py
class multipleFileUpload(APIView):
def post(self, request):
"""
:param request:
:return:
"""
try:
#this will read attributes other than file
str_value = request.POST["attributes"]
print(str_value)
#check if any file send in request if yes read all files one by one
if len(request.FILES) != 0:
for key in request.FILES:
file_obj = request.FILES[key]
print(file_obj.read()) #getting contents of the file in bytes
return JsonResponse({"res": "got files"})
except Exception as e:
print(str(e))
return JsonResponse({"res": "error"})
將此行添加到您的urls.py中
url(r'uploadFiles / $',views.multipleFileUpload.as_view(),name ='uploadFiles'),
使用相同的參數運行您的Post man,讓我知道。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.