-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24d74fb
commit c841cbe
Showing
2 changed files
with
37 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from microdot_asyncio import Microdot, send_file, Request | ||
|
||
app = Microdot() | ||
Request.max_content_length = 1024 * 1024 # 1MB (change as needed) | ||
|
||
|
||
@app.get('/') | ||
async def index(request): | ||
return send_file('index.html') | ||
|
||
|
||
@app.post('/upload') | ||
async def upload(request): | ||
# obtain the filename and size from request headers | ||
filename = request.headers['Content-Disposition'].split( | ||
'filename=')[1].strip('"') | ||
size = int(request.headers['Content-Length']) | ||
|
||
# sanitize the filename | ||
filename = filename.replace('/', '_') | ||
|
||
# write the file to the files directory in 1K chunks | ||
with open('files/' + filename, 'wb') as f: | ||
while size > 0: | ||
chunk = await request.stream.read(min(size, 1024)) | ||
f.write(chunk) | ||
size -= len(chunk) | ||
|
||
print('Successfully saved file: ' + filename) | ||
return '' | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run(debug=True) |