Skip to content

Instantly share code, notes, and snippets.

@wwnbb
Created November 7, 2023 08:21
Show Gist options
  • Select an option

  • Save wwnbb/42629f0a8b1e0f4523ce6a8a5b9b2ed2 to your computer and use it in GitHub Desktop.

Select an option

Save wwnbb/42629f0a8b1e0f4523ce6a8a5b9b2ed2 to your computer and use it in GitHub Desktop.
upload_and_sumbit.py
from typing import List
from fastapi import FastAPI, UploadFile, File, Form
from uuid import uuid4
app = FastAPI()
# Simulate a database for storing file references
file_db = {}
@app.post("/upload-files/")
async def upload_files(files: List[UploadFile] = File(...)):
file_ids = []
for file in files:
# Generate a unique file ID
file_id = str(uuid4())
file_ids.append(file_id)
# In a real application, you would save the file to a storage system here
file_db[file_id] = file.filename
return {"file_ids": file_ids}
@app.post("/submit-form/")
async def submit_form(data: str = Form(...), file_ids: List[str] = Form(...)):
# Here, handle the received data along with file IDs
# In a real application, you would probably associate the file IDs with the data in a database
return {"data": data, "file_references": [file_db[file_id] for file_id in file_ids if file_id in file_db]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment