As it says in the title, I am trying to upload a model.pkl and save it in an API made in django. I manage to save the model.pkl correctly in the API, however the file is uploaded in a corrupt way, because I cannot read the pickle file.
Im open to any solutions that can make the pickle file be uploaded, stored and readed in the API.
Class to upload the file and save it
class FileUploadView(APIView):
parser_classes = (FileUploadParser,)
def put(self, request):
file = request.data.get('file', None)
file_name = f'path/{file.name}'
path = default_storage.save(name=file_name, content=ContentFile(file.read()))
tmp_file = os.path.join(settings.MEDIA_ROOT, path)
if file is not None:
return Response(f'File: {file.name} successfully uploaded and saved!', status=HTTP_200_OK)
else:
return Response(f'File not found!', status=HTTP_400_BAD_REQUEST)
Error when reading the file
def read_PickleModel(path_to_PickleModel):
with open(path_to_PickleModel, 'rb') as f:
pickle_model = pickle.load(f)
return pickle_model
read_PickleModel(path_to_PickleModel)
Traceback:
DEBUG: An exception has ocurred: invalid load key, '-'.
Im new in this, please before voting down, tell me how do you think I can improve the question to get an accurate solution.
Postman
CodePudding user response:
To set the filename you need to set a header "Content-Disposition" and pass attachment; filename=<your_filename.ext>