Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

670
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda