question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Internal server error while trying to get uploaded file name in FastAPI

See original GitHub issue

What I am trying is to upload an image using html template and to create a copy of the image by generating random names and to display the new name in a webpage.

from fastapi import FastAPI,File, UploadFile, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
import shutil
import os
import datetime
import string
import random


A = datetime.datetime.now()
N = 10
ran = ''.join(random.choices(string.ascii_uppercase + string.digits, k= N))

           


app = FastAPI()

app.mount("/static", StaticFiles(directory="static"), name="static")


templates = Jinja2Templates(directory="templates")


@app.get("/uploadfile", response_class=HTMLResponse)
async def read_item(request: Request ):
    return templates.TemplateResponse("imageupload.html", {"request": request})

@app.post("/uploadfile")
async def create_image(request:Request,image: UploadFile = File(...)):
    abc = os.path.splitext(image.filename)[-1]
    print(abc)
    newname =image + str(A) + ran + abc
    print(newname)
    image.filename = newname
    imagepath = "/static/media/" + image.filename
    print(imagepath)

    with open(os.path.join("./static/media/",image.filename),"wb+") as buffer:
        shutil.copyfileobj(image.filename, buffer)
    return templates.TemplateResponse("imagename.html",{"request": request,"image_path":imagepath})

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
iamrahulrameshcommented, Nov 23, 2020

Please edit your code with proper formatting, so others can easily test and debug your code.

I think I did now. I am trying to learn fastapi , What I am trying is to upload an image using html template and to create a copy of the image by generating random names and to display the new name in a webpage

0reactions
webdaggercommented, May 10, 2022

Please look at the documentation for UploadFile: https://fastapi.tiangolo.com/tutorial/request-files#uploadfile

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python FastAPI: Uploading Text file gives internal server error
from fastapi import FastAPI, UploadFile, File app = FastAPI() ... file.filename return response def readTxt(file): return file.read().
Read more >
Request Files - FastAPI
To receive uploaded files, first install python-multipart . ... filename : A str with the original file name that was uploaded (e.g. myimage.jpg...
Read more >
Question: How to upload a file using HTTP POST Form-Data?
I want to upload a file using HTTP POST where username and password are to be filled using form-data. It works well from...
Read more >
How to Save Uploaded Files in FastAPI | by Ng Wai Foong
Step-by-step guide to receive files and save them locally. Image by Author ... You can easily implement it inside FastAPI server.
Read more >
Resolve API Gateway Lambda stage variable 500 errors
Why do I get an "Internal server error" and a 500 status code when I invoke the API method? Last updated: 2021-06-21. I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found