I run my Python webapi application at localhost:5000 address on my local computer (MacOS Ventura)
I created a directory named "receivedfiles", where myapp.py is located. I can reach my app via localshot:5000 (or 192.168.1.2 instead of localhost) and upload a file to the receivedfiles directory via my webapi application.
Here is the python code for file upload, in my webapi application
@app.route('/api/uploadfile', methods=['GET','POST'])
def uploadafile():
if 'myfile' not in request.files:
return 'file could not be uploaded.', 400
myfile = request.files['myfile']
if myfile.filename == '':
return 'please specify the filename', 400
# save the file to the receivedfiles folder
myfile.save('receivedfiles/' + myfile.filename)
return 'successfully uploaded', 200
this code works properly.
Here is the relative tree:
.../myapp.py > my python webapi runs
.../receivedfiles/ > a directory for files
.../receivedfiles/sampleimage1.png > a file under that directory
192.168.1.2:5000/receivedfiles/ > relative path under localhost (192.168.1.2)
Users/myusername/myprojects/mypyhtonprojects/webapiproject1/receivedfiles/ is the absoulte (physical) path. I can access to the file via file:///Users/myusername/myprojects/mypyhtonprojects/webapiproject1/receivedfiles/sampleimage1.png on browser.
But when I try to directly access the file by 192.168.1.2:5000/receivedfiles/sampleimage1.png (I tried without port number too) from the browser and/or e.g. my flutter application, it returns "not found" messaage.
How can I access to the files via localhost (or /receivedfiles path and the files under that directory?
PS : I reached the folder via physical path and shared to any access.
Thank you for any help.
The web server doesn't have access to all the files in your project. You should integrate Flask into this project to designate the desired files or folders as static, so that the web server can access it. It should be as easy as this
`
'
send_from_directory is a Flask function to serve files from a specified directory. The new route /uploads/ allows you to access files like: 192.168.1.2:5000/uploads/sampleimage1.png