jclyo1 commited on
Commit
be4dbd7
1 Parent(s): b0f2b9f
Files changed (2) hide show
  1. main.py +19 -1
  2. static/index.html +1 -1
main.py CHANGED
@@ -1,6 +1,7 @@
1
- from fastapi import FastAPI, UploadFile
2
  from fastapi.staticfiles import StaticFiles
3
  from fastapi.responses import FileResponse
 
4
 
5
  import subprocess
6
  import os
@@ -86,6 +87,23 @@ def verify_image(file: UploadFile):
86
  open(fn, 'wb').write(fileitem.file.read())
87
 
88
  return {"response": fileitem.filename}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  app.mount("/", StaticFiles(directory="static", html=True), name="static")
90
 
91
 
 
1
+ from fastapi import FastAPI, Request, UploadFile, HTTPException, status
2
  from fastapi.staticfiles import StaticFiles
3
  from fastapi.responses import FileResponse
4
+ import aiofiles
5
 
6
  import subprocess
7
  import os
 
87
  open(fn, 'wb').write(fileitem.file.read())
88
 
89
  return {"response": fileitem.filename}
90
+
91
+ @app.post('/upload')
92
+ async def upload(fileUpload: UploadFile):
93
+ try:
94
+ contents = await fileUpload.read()
95
+ async with aiofiles.open(fileUpload.filename, 'wb') as f:
96
+ await f.write(contents)
97
+ except Exception:
98
+ raise HTTPException(
99
+ status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
100
+ detail='There was an error uploading the file',
101
+ )
102
+ finally:
103
+ await fileUpload.close()
104
+
105
+ return {'message': f'Successfuly uploaded {fileUpload.filename}'}
106
+
107
  app.mount("/", StaticFiles(directory="static", html=True), name="static")
108
 
109
 
static/index.html CHANGED
@@ -22,7 +22,7 @@
22
  </head>
23
  <body>
24
  <div class="container">
25
- <form id="fileUploadForm" enctype = "multipart/form-data">
26
  <label class="form-label" for="customFile">Default file input example</label>
27
  <input type="file" class="form-control" name="fileUpload" id="fileUpload" />
28
  <button class="btn btn-primary" type="submit">Submit</button>
 
22
  </head>
23
  <body>
24
  <div class="container">
25
+ <form action="/upload" id="fileUploadForm" enctype = "multipart/form-data">
26
  <label class="form-label" for="customFile">Default file input example</label>
27
  <input type="file" class="form-control" name="fileUpload" id="fileUpload" />
28
  <button class="btn btn-primary" type="submit">Submit</button>