aboutsummaryrefslogtreecommitdiffstats
path: root/docs/api-guide/parsers.md
diff options
context:
space:
mode:
authorTom Christie2013-05-07 05:09:09 -0700
committerTom Christie2013-05-07 05:09:09 -0700
commit642970a1b8e6ebadbbfc9da4d75fad1ec5da6747 (patch)
tree42f0654be36b606f41dbad5259e7b534f0aa350b /docs/api-guide/parsers.md
parent5356af8651fccacf5524add33569dd84d9e78646 (diff)
parent5faaba9c691851ec68e385cc87d6bce82e4d4853 (diff)
downloaddjango-rest-framework-642970a1b8e6ebadbbfc9da4d75fad1ec5da6747.tar.bz2
Merge pull request #806 from wronglink/master
Added FileUploadParser
Diffstat (limited to 'docs/api-guide/parsers.md')
-rw-r--r--docs/api-guide/parsers.md51
1 files changed, 22 insertions, 29 deletions
diff --git a/docs/api-guide/parsers.md b/docs/api-guide/parsers.md
index 49d59d10..20518647 100644
--- a/docs/api-guide/parsers.md
+++ b/docs/api-guide/parsers.md
@@ -102,6 +102,28 @@ You will typically want to use both `FormParser` and `MultiPartParser` together
**.media_type**: `multipart/form-data`
+## FileUploadParser
+
+Parses raw file upload content. Returns a `DataAndFiles` object. Since we expect the whole request body to be a file content `request.DATA` will be None, and `request.FILES` will contain the only one key `'file'` matching the uploaded file.
+
+The `filename` property of uploaded file would be set to the result of `.get_filename()` method. By default it tries first to take it's value from the `filename` URL kwarg, and then from `Content-Disposition` HTTP header. You can implement other behaviour be overriding this method.
+
+Note that since this parser's `media_type` matches every HTTP request it imposes restrictions on usage in combination with other parsers for the same API view.
+
+Basic usage expamle:
+
+ class FileUploadView(views.APIView):
+ parser_classes = (FileUploadParser,)
+
+ def put(self, request, filename, format=None):
+ file_obj = request.FILES['file']
+ # ...
+ # do some staff with uploaded file
+ # ...
+ return Response(status=204)
+
+**.media_type**: `*/*`
+
---
# Custom parsers
@@ -145,35 +167,6 @@ The following is an example plaintext parser that will populate the `request.DAT
"""
return stream.read()
-## Uploading file content
-
-If your custom parser needs to support file uploads, you may return a `DataAndFiles` object from the `.parse()` method. `DataAndFiles` should be instantiated with two arguments. The first argument will be used to populate the `request.DATA` property, and the second argument will be used to populate the `request.FILES` property.
-
-For example:
-
- class SimpleFileUploadParser(BaseParser):
- """
- A naive raw file upload parser.
- """
- media_type = '*/*' # Accept anything
-
- def parse(self, stream, media_type=None, parser_context=None):
- content = stream.read()
- name = 'example.dat'
- content_type = 'application/octet-stream'
- size = len(content)
- charset = 'utf-8'
-
- # Write a temporary file based on the request content
- temp = tempfile.NamedTemporaryFile(delete=False)
- temp.write(content)
- uploaded = UploadedFile(temp, name, content_type, size, charset)
-
- # Return the uploaded file
- data = {}
- files = {name: uploaded}
- return DataAndFiles(data, files)
-
---
# Third party packages