Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.

support multipart/form-data (e.g. file uploads) - #44

Open
chrisv2 wants to merge 1 commit into
core-api:masterfrom
greybyte:add_multipart_encoding
Open

support multipart/form-data (e.g. file uploads)#44
chrisv2 wants to merge 1 commit into
core-api:masterfrom
greybyte:add_multipart_encoding

Conversation

@chrisv2

@chrisv2chrisv2 commented Nov 16, 2017

Copy link
Copy Markdown

This patch enables file uploads by allowing multipart/form-data encoding in transport.

Usage example

(this took me a while to figure out, feel free to add this to the docs)

Server (Django)

fromrest_frameworkimportserializers, viewsets, parsers# serializer with file fieldclassAttachmentSerializer(serializers.Serializer):
file=serializers.FileField(write_only=True)
# viewset with upload methodclassMyViewset(viewsets.ModelViewSet):
# ...@detail_route(methods=['post'],serializer_class=AttachmentSerializer, parser_classes=(parsers.MultiPartParser,))defadd_attachment(self, request, *args, **kwargs):
serializer=serializers.AttachmentSerializer(data=request.data)
ifserializer.is_valid():
# serializer.validated_data['file'] is an instance of django.core.files.Fileprint("uploaded file: %s"%serializer.validated_data['file']

Client (JS)

constclient=newcoreapi.Client()constschema=window.schema// file must be either an instance of Blob (for content generated in JS) or // an instance of File (retrieved from an <input type="file"> or a dropEvent.FileList)client.action(schema,['mymodel','add_attachment'],{id: id,file: file}).then((result)=>console.log(result))

Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@chrisv2