[QUESTION] Is this the correct way to save an uploaded file ?
See original GitHub issueHello, i am trying to save an uploaded file to disk, the following code works correctly but i wonder if it is the correct way to do that.
def parse(file: UploadFile = File(...)):
extension = os.path.splitext(file.filename)[1]
_, path = tempfile.mkstemp(prefix='parser_', suffix=extension)
with open(path, 'wb') as f:
f.write(file.file.read())
Basically i need to get the path of the temp file. I also have tried with .rollover() but file.file.name does not return the path (only the file descriptor)
Issue Analytics
- State:
- Created 4 years ago
- Comments:21 (10 by maintainers)
Top Results From Across the Web
How to save a file after upload | OutSystems
How to save a file after upload · 1) Create a new Web application i.e. Traditional web application · 2) Create a module...
Read more >Add a “file upload” question to a Google Form - YouTube
Looking for an easy way to upload a file to a Google Form ? In this episode of The Suite Life, Laura Mae...
Read more >How to save uploaded file in JSF - Stack Overflow
You should first prepare a folder on the local disk file system where the uploaded files should be stored. For example, /path/to/uploads (on ......
Read more >Upload and save files and folders to OneDrive
Select the files you want to upload, and drag them to OneDrive in the File Explorer Navigation pane. Select File > Save a...
Read more >File Upload Question - Qualtrics
Stop betting on what your employees and customers want and find out why they contact you, how they feel and what they will...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@classywhetten FastAPI has almost no custom logic related to
UploadFile
– most of it is coming from starlette. So if you want a “save” wrapper or a better example of usage, it probably makes sense to ask in the starlette repo.@damianoporta This line:
causes the file with location
path
to actually be opened by python, which is what @vitalik was referring to when he said you need to close the tempfile handle (which is the thing named_
in your code).Here are some utility functions that the people in this thread might find useful (at least as a starting point):
(I haven’t tested the above functions, just adapted them from slightly more complex functions in my own code.)
Note: you’d want to use the above functions inside of
def
endpoints, notasync def
, since they make use of blocking APIs.I don’t know whether it handles out-of-memory issues reasonably well or not, but that is not an issue in my application – maximum request size is capped elsewhere, and the volume of concurrent requests I’m currently handling is such that OOM isn’t really an issue.
I’ve been digging through Flask’s documentation, and their file.save function is a wrapper around shutils.copyfileobj() in the standard library.
Here’s my working code:
I would much appreciate a wrapper around this be built into fastapi