The content of the Http uploaded file obtained by Python's Django-rest-framework framework is incorrect after the file is saved.

after getting the uploaded file, write the uploaded file stream to the server file. The file contains more http stream related information

--X-INSOMNIA-BOUNDARY
Content-Disposition: form-data; name="file"; filename="a.txt"
Content-Type: text/plain



Hello




--X-INSOMNIA-BOUNDARY--

how to get uploaded file information when saved, it will only be the original file.

< hr >

add my source code:


class UploadView(APIView):
    """
    
    """

    parser_classes = (FileUploadParser, )

    def put(self, request, filename, format = None):
        """
        
        """

        up_file = request.data["file"]
        base_dir = settings.BASE_DIR

        print filename


        storage = base_dir + "/" + "storage/"
        new_file = storage + up_file.name

        with open(new_file, "wb+") as destination:
            for chunk in up_file.chunks():
                destination.write(chunk)

            destination.close()

        -sharp up_file.new_file("file", new_file, up_file.content_type, up_file.size, up_file.charset, up_file.content_type_extra)


        -sharp with open(new_file, "wb+") as destination:
        -sharp     for line in up_file:
        -sharp         destination.write(line)
        -sharp     destination.close()
        -sharp     -sharp destination.write(up_file.multiple_chunks())
        -sharp     -sharp destination.close()

        return Response(up_file.name, status.HTTP_201_CREATED)

see that your saved content should be uploaded to the server using a class like BaseHTTPServer , which comes with python.
python/blob/master/py2016/SimpleHTTPServerWithUpload.py" rel=" nofollow noreferrer "> https://github.com/smilejay/p.
if this is the case, there is no good way to deal with the information related to the http stream. We can only read it line by line and manually remove the information related to http in a way like readline .
fortunately, these are all http information with relevant standards, such as 1-4 behavior http information, 5-(- 2) behavior upload information, and the last behavior http information (this is just a random example)
, so if you want to upload files in this way, You need to study the rfc standard yourself:
http://www.ietf.org/rfc/rfc1867
http://www.vivtek.com/rfc1867.

but look at the django on your tag, so it should be easier and easier to use the request.FILES of django .
reference address:
https://docs.djangoproject.co.
example:

-sharp 
request.FILES['filename'].name
-sharp 
for filename, file in request.FILES.iteritems():
    name = request.FILES[filename].name
    -sharp 
    file.readall()
Menu