From c623155d8698a6480d6363e5c8a2ea05947c6c6e Mon Sep 17 00:00:00 2001 From: "maxiaozhe@alicorns.co.jp" Date: Tue, 11 Jul 2023 21:21:46 +0900 Subject: [PATCH] =?UTF-8?q?CORS=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/api_v1/routers/kintone.py | 23 +++++++++++------------ backend/app/main.py | 15 ++++++++++++++- frontend/src/components/DocUpload.vue | 1 + 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/backend/app/api/api_v1/routers/kintone.py b/backend/app/api/api_v1/routers/kintone.py index 75d5634..b6a9831 100644 --- a/backend/app/api/api_v1/routers/kintone.py +++ b/backend/app/api/api_v1/routers/kintone.py @@ -8,16 +8,15 @@ kinton_router = r = APIRouter() @r.post("/upload",) -async def upload(files:t.List[UploadFile] = File(...)): +async def upload(file:UploadFile = File(...)): dataframes = [] - for file in files: - if file.filename.endswith('.xlsx'): - try: - content = await file.read() - df = pd.read_excel(BytesIO(content)) - dataframes.append(df) - except Exception as e: - raise HTTPException(status_code=400, detail=f"Error occurred while parsing file {file.filename}: {str(e)}") - else: - raise HTTPException(status_code=400, detail=f"File {file.filename} is not an Excel file") - return {"files": [file.filename for file in files]} + if file.filename.endswith('.xlsx'): + try: + content = await file.read() + df = pd.read_excel(BytesIO(content)) + dataframes.append(df) + except Exception as e: + raise HTTPException(status_code=400, detail=f"Error occurred while parsing file {file.filename}: {str(e)}") + else: + raise HTTPException(status_code=400, detail=f"File {file.filename} is not an Excel file") + return {"file": file.filename} diff --git a/backend/app/main.py b/backend/app/main.py index 981e64b..aade6f0 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -9,12 +9,25 @@ from app.core import config # from app.core.auth import get_current_active_user from app.core.celery_app import celery_app from app import tasks - +from fastapi.middleware.cors import CORSMiddleware app = FastAPI( title=config.PROJECT_NAME, docs_url="/api/docs", openapi_url="/api" ) +origins = [ + "http://localhost:9000", + "http://localhost", + "http://localhost:8080", +] + +app.add_middleware( + CORSMiddleware, + allow_origins=origins, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) # @app.middleware("http") # async def db_session_middleware(request: Request, call_next): diff --git a/frontend/src/components/DocUpload.vue b/frontend/src/components/DocUpload.vue index bdbdcaa..2ab3c1b 100644 --- a/frontend/src/components/DocUpload.vue +++ b/frontend/src/components/DocUpload.vue @@ -6,6 +6,7 @@ :label="title" accept=".csv,.xlsx" :on-rejected="onRejected" + field-name="file" >