Gasta153
Gasta153

Reputation: 11

corrupted files uploading csv and zip files via postman toward django rest api


Here my model:

class FileUploader(models.Model):
id_file = models.AutoField('id_file', primary_key=True)
Campaign_Name = models.CharField('Campaign_Name', max_length=255)
ViewpointSurvey = models.FileField('ViewpointSurvey',upload_to=path_and_rename_base,max_length=255,blank=False,null=False,db_column='ViewpointSurvey', name='ViewpointSurvey')
ProjectSurvey = models.FileField('ProjectSurvey', upload_to=path_and_rename_base, max_length=255, blank=False, null=False,
                        db_column='ProjectSurvey', name='ProjectSurvey')
Trajectories = models.FileField('Trajectories', upload_to=path_and_rename_base, max_length=255, blank=False, null=False,
                        db_column='Trajectories', name='Trajectories')
screenshots = models.FileField('screenshots', upload_to=path_and_rename_base, max_length=255, blank=False,
                                null=False,
                                db_column='screenshots', name='screenshots')
timestamp=models.DateTimeField('timestamp',auto_now_add=True,db_column='timestamp', name='timestamp')
id_project=models.CharField('id_project', max_length=255)
class Meta:
    db_table = "file_uploader"
    verbose_name_plural = 'file_uploader'
    verbose_name = "file_uploader"

Here the view:

class CSVUploadAPI(GenericAPIView):
parser_classes = [MultiPartParser]
serializer_class = UploadSerializer

def put(self, request):
    data_collect = request.data
    serializer = UploadSerializer(data=data_collect)
    if serializer.is_valid():
        serializer.save()
        return Response('Done')
    else:
        return Response(UploadSerializer.errors,status=status.HTTP_400_BAD_REQUEST)

Here the serializer:

class UploadSerializer(serializers.ModelSerializer):
class Meta:
    model= FileUploader
    fields = ('id_project','ProjectSurvey','Trajectories','ViewpointSurvey','screenshots','Campaign_Name')

Here the path_and_rename_base function:

def path_and_rename_base(instance, filename):
upload_to = 'files/'
ext = filename.split('.')[-1]
# set filename as random string
filename = '{}.{}'.format(uuid.uuid4(), ext)    
return os.path.join(upload_to, filename)

Here the postman headers postman headers

Here the postman body postman body

Here the file list with sizes Highlighted file is the only one uploaded correctly

Upvotes: 1

Views: 190

Answers (0)

Related Questions