0

Got error

The submitted data was not a file. Check the encoding type on the form.

My code looks like follow:

Model:

class Document(models.Model):
    name = models.CharField(max_length=100, verbose_name=_('Name'))
    date = models.DateField(verbose_name=_('Date'))
    project = models.ForeignKey(Project, verbose_name=_('Project'), related_name='projects')
    type = models.CharField(max_length=50, verbose_name=_('Document Type'))
    docfile = models.FileField(upload_to='documents/%Y/%m/%d', blank=True, verbose_name=_('Document'))

    def __unicode__(self):
        return self.name

My code for serializer:

class DocumentSaveSerializer(serializers.ModelSerializer):
    date = serializers.DateField()

    class Meta:
        model = Document
        fields = ('name', 'date', 'project', 'type', 'docfile')

My views:

class DocumentViewSet(WithNestedSerializerMixin, viewsets.ModelViewSet):
    serializer_class = DocumentSerializer
    queryset = Document.objects.all()

    get_object_serializer_class = DocumentSerializer
    post_serializer_class = DocumentSaveSerializer
    put_serializer_class = DocumentPutSerializer

    parser_classes = (FileUploadParser,)

    def post(self, request, *args, **kwargs):
        docfile = request.FILES['docfile']

And some code from frontend:

Directive: cmsApp.directive(
    'contracts',
    [
        'ContractService',
        '$rootScope',
        function (ContractService, $rootScope) {
            return {
                restrict: 'A',
                scope: {
                    contract: '=',
                    projectId: '=',
                    typeId: '=',
                    docFile: '='
                },
                templateUrl: 'main/templates/contract.html',
                link: function (scope, element, attrs) {
                scope.newContract = function(){
                    scope.contract.document.project = scope.projectId;
                    scope.contract.document.type = scope.typeId;
                    scope.contract.document.docfile = scope.docFile;
                    ContractService.createContract(scope.contract)
                    scope.contract.document = {};
                };
                }
            }
        }
    ]
)

And finally HTML:

<input type="file" file-model="docfile">

<span ng-if="DocumentName == 'Contract'" contracts contract="Contract" project-id="ProjectID" type-id="DocumentName" doc-file="docfile"></span>

Dont have any ideas what is wrong with this code. Anny suggestions? File is loaded properly. I test it with showing name, size etc. But when i try to POST i've got an error I mentioned at the benning.

Thanks for help

1
  • did you add enctype='multipart/form-data' in your form tag? Commented Mar 17, 2015 at 16:01

1 Answer 1

1

If you are submitting your file via form, you need to add enctype='multipart/form-data' to your <form> tag

Otherwise, if you are using $http angular service, you need add this header.

headers: {'Content-Type': 'multipart/form-data'}

It should looks like this:

$http({
   method: 'POST',
   url: '/your_post/url',
   data: data_with_your_file// your original form data,
   headers: {'Content-Type': 'multipart/form-data'}
}).

BUT

I highly recommend using angular-file-upload.js to upload files in angular. Is very clean, I use it with DRF too.

Sign up to request clarification or add additional context in comments.

2 Comments

got something like this: return $http({ method: 'POST', url: url, data: {contract: newContract.document.docfile}, // your original form data, headers: {'Content-Type': 'multipart/form-data'} }) But now I have error 500
@bMh 500 error, is server error, check your server logs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.