Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

663
Vistas
How to rename uploaded file with fastapi

I have an endpoint that allows users to upload files. I would like to rename the file to a random and unique file name but can't work out how to change the filename. My current code is as follows which is saving the file to files/filename

@router.post('/qualifications/file')
async def create_file(files: UploadFile = File(...)):
   file_location = f"files/{files.filename}"
   with open(file_location, "wb+") as file_object:
      file_object.write(files.file.read())
      return {"info": f"file '{files.filename}' saved at '{file_location}'"}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Can you generate a custom file location instead of using files.filename? You may have to work out what the files.filename extension was and pass that into the new file_name you are creating.


@router.post('/qualifications/file')
async def create_file(files: UploadFile = File(...)):\

   file_name = "some_random_name.txt"

   file_location = f"files/{file_name}"

   with open(file_location, "wb+") as file_object:
      file_object.write(files.file.read())
      return {"info": f"file '{file_name}' saved at '{file_location}'"}



over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda