I want to upload a large ZIP file using FastAPI. I need to validate whether the uploaded file is a ZIP file before proceeding. While using the UploadFile class in FastAPI to handle file uploads, I want to first check if the file is indeed a ZIP file. If it is, then I will upload it. If it's not a ZIP file, I want to return an error message stating that the file is not a ZIP file, without fully processing the multipart upload of the large file.
from fastapi.responses import HTMLResponse
import os
app = FastAPI()
UPLOAD_FOLDER = 'uploads/'
ALLOWED_EXTENSIONS = {'csv'}
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
def allowed_file(filename: str) -> bool:
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.post("/uploadfile/")
async def upload_file(file: UploadFile = File(...)):
if not allowed_file(file.filename):
raise HTTPException(status_code=400, detail="Invalid file type. Only CSV files are allowed.")
file_location = os.path.join(UPLOAD_FOLDER, file.filename)
with open(file_location, "wb") as buffer:
buffer.write(await file.read())
return {"info": f"File '{file.filename}' uploaded successfully"}