Skip to content

Commit f972448

Browse files
committed
Feat : cors 에러 해결
1 parent f58cdd0 commit f972448

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

corsTest.html

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>이미지 업로드</title>
5+
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
6+
</head>
7+
<body>
8+
<h1>이미지 업로드</h1>
9+
<input type="file" id="imageInput">
10+
<button onclick="uploadImage()">이미지 업로드</button>
11+
12+
<script>
13+
function uploadImage() {
14+
var input = document.getElementById('imageInput');
15+
var image = input.files[0];
16+
var file = new FormData();
17+
file.append('file', image);
18+
console.log("formData : %o", file);
19+
var requestOptions = {
20+
method: 'POST',
21+
body: file,
22+
redirect: 'follow'
23+
};
24+
25+
axios.post('http://127.0.0.1:8000/photo', file)
26+
.then(function (response) {
27+
// 업로드에 성공한 경우
28+
console.log(response.data);
29+
// 응답 데이터를 처리하거나 화면에 표시합니다.
30+
})
31+
.catch(function (error) {
32+
// 업로드에 실패한 경우
33+
console.log("에러네..");
34+
console.log(error);
35+
// 에러 처리 로직을 추가합니다.
36+
});
37+
}
38+
</script>
39+
</body>
40+
</html>

main.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,28 @@
44
from PIL import Image
55
import torch
66
import os
7+
from fastapi.middleware.cors import CORSMiddleware
78

89
from loguru import logger
910

1011

1112
model = torch.load('shufflenet_weight.pt', map_location=torch.device('cpu'))
1213

14+
1315
app = FastAPI()
1416

17+
origins = [
18+
"*"
19+
]
20+
21+
app.add_middleware(
22+
CORSMiddleware,
23+
allow_origins=origins,
24+
allow_credentials=True,
25+
allow_methods=["*"],
26+
allow_headers=["*"],
27+
)
28+
1529

1630
@app.post("/photo")
1731
async def create_upload_file(file: UploadFile = File(...)):
@@ -28,6 +42,10 @@ async def create_upload_file(file: UploadFile = File(...)):
2842
# image = ToTensor()(Image.open(file.file))
2943
# output = model(image)
3044
# logger.info("Classify Result = {}", output)
45+
print({
46+
"content_type": file.content_type,
47+
"filename": file.filename
48+
})
3149
return {
3250
"content_type": file.content_type,
3351
"filename": file.filename

0 commit comments

Comments
 (0)