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

173 Views Asked by At
  • Hi, I'm using django 4.0.6 (python 3.8) with djangorestframework 3.13.1 on windows. I'm testing on localhost my app with postman and postman agent up to date.
  • The main goal is to send 3 csv file, 1 zip file end 2 textfield information
  • The authorization JWT is working fine.
  • The shortest csv is uploaded correctly
  • If I upload a long csv or zip files they result in corrupted files. Only short csv files are correctly uploaded,
  • long csv files result in corrupted lines on the lower part of the text with this sort of characters: Û™eÞqHÂÔpŠl°‹<û yj­Ïª›kÃx›Ûr-¥x¡S¼à2SÕkÛår'mÒÕµd5ÿ¶Vê0@1 ̦Záë1§ŠIÇaÎ
    “’ÏÛ€t»vRoT"·‡Qf„¾´é-Oa)]ЧK‹5C¤sWB0),3 Zž—2¸Ñóo«jŸH“
  • I can't figure out how to fix it, reading other posts I couldn't find a solution that fit with this situation.
  • Thank You for any suggestion!

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

0

There are 0 best solutions below