K
K
Konstantin Malyarov2018-06-18 14:39:47
Django
Konstantin Malyarov, 2018-06-18 14:39:47

How to upload a file using DRF + VueJS?

JS

new Vue({
    delimiters: ["{>", "<}"],
    el: '#import_tokens',
    data() {
        return {
            file: null,
        }
    },
    methods: {
        sendFile: function () {
            var self = this
            console.log(this.file)
            axios.defaults.xsrfCookieName = 'csrftoken'
            axios.defaults.xsrfHeaderName = 'X-CSRFToken'
            axios.put(
                'http://' + window.location.host + '/api/tokens_VMP_excel/',
                self.file,
                {
                    headers: {
                        'Content-Type': 'multipart/form-data'
                    }
                }
            ).then(function (response) {
            }).catch(function (error) {
                console.log(error);
            });
        }
    },
    created: function () {

    }
})

PY
class TokensVMPExcelViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = TokenVMPExcel.objects.all()
    serializer_class = TokensVMPExcelSerializer
    parser_classes = (MultiPartParser,)

    def put(self, request):
        print (request.FILES)
        return Response(status=204)

The print is empty.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
planc, 2018-06-18
@planc

What's a handy, beautiful file uploader for vue?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question