Internal server error while trying to get uploaded file name in FastAPI
See original GitHub issueWhat 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:
- Created 3 years ago
- Comments:12 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
Please look at the documentation for UploadFile: https://fastapi.tiangolo.com/tutorial/request-files#uploadfile