Tuesday, 15 June 2010

angularjs - Upload using angular-file-model and rest_framework -


gate error

  submitted data file was not checked on the form encoding type  

My code looks like:

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', empty = true, verbose_name = _ ( 'Document')) def __uni Code __ (self): Return self.name  

My code for serializer:

Document DocumentSaveSerializer (serializers.ModelSerializer): date

My thoughts: / P>

  Class DocumentViewSet (withNestedSerializerMixin, viewsets.ModelViewSet): serializer_class = DocumentSerializer queryset = Document.objects. (All, request, * args, ** kwargs): docfile = request.FILES ['doctor']  

and some code from foreground:

  Director: cmsApp.directive ('contract', ['contract service', '$ rootsecope', function (contract service, $ rootsecope) {return (limited: 'a', scope: {contract: '=', project id : '=', Type id: '=', docFile: '='}, Template: 'main / template / contract.html', link: function (region, element, ethers) {scope.newContract = function () { Scope.contract.document.project = scope.projectId; Scope.contract.document.type = scope.typeId; Scope.con Tract.document.doc.dllfile = scope.docFile; Contract SevaContract (scope.contract) scope.contract.document = {};}}}}}]]  

and finally HTML: < / P>

  & lt; input type = "file" file-model = "docfile" & gt; & lt; span ng-if = "DocumentName == 'contract'" contract contract = "contract" Project-ID = "Project ID" type-id = "document name" doctor-file = "documentfile" & gt; & lt; / span & gt;  

Do not think about what is wrong with this code. Anny Suggestions? The file is properly loaded. I test it with the name, size and so on. But when I try to post, I get an error.

Thanks for the help

If you are submitting your file via form , Then you have to add enctype = 'multipart / form-data' to your & lt; Form & gt; Tags

Otherwise, if you are using the $ http Angular service, then you need to add this header.

  Header: {'Content-Type': 'Multipart / Form-Data'}  

It should look 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 suggest uploading files in the coyol, it is very clear, I also use it with DRF


No comments:

Post a Comment