Created
November 7, 2023 08:21
-
-
Save wwnbb/42629f0a8b1e0f4523ce6a8a5b9b2ed2 to your computer and use it in GitHub Desktop.
upload_and_sumbit.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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