Flask Upload Image works only on Localhost, not in Online Server
Answer a question
Good day all. I have a little issue on uploading image using flask. I can upload image on flask in localhost (the web app is hosted on my desktop). But when I loaded the flask app on Online Server, I always encounter the "FileNotFoundError". I also change the permission to 777 but still not working.
Here is the html upload code.
<form name="edit_vehicle_info" action="/vehicle_info_form/" method="POST" enctype="multipart/form-data" class="formfield">
<div class="form-group">
<label for="changeVehicleImg">Vehicle Image</label>
<input type="file" id="changeVehicleImg" name="changeVehicleImg" accept="image/*">
</div>
Here is the python upload code.
#vehicle_info_form
@app.route('/vehicle_info_form/', methods=['GET', 'POST'])
def vehicle_info_form():
try:
if request.method == "GET":
return render_template(
'vehicleInfo.html'
)
elif request.method == "POST":
inputVehicleImg = request.files['changeVehicleImg']
if inputVehicleImg.filename == "":
inputVehicleImg_filename = ""
else:
print(inputVehicleImg.filename)
print(app.config['UPLOAD_FOLDER_IMG_VEHICLE'])
inputVehicleImg_filename = secure_filename(inputVehicleImg.filename)
inputVehicleImg.save(os.path.join(app.config['UPLOAD_FOLDER_IMG_VEHICLE'], inputVehicleImg_filename))
print ('success')
except Exception as e:
print(e)
return redirect(url_for('vehicle_info_form'))
The python upload code can print the inputVehicleImg.filename and app.config['UPLOAD_FOLDER_IMG_VEHICLE'] then after that, the error occurs.
Here is the error.
Traceback (most recent call last):
File "/home/ubuntu/Projects/FMS/lib/python3.6/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/home/ubuntu/Projects/FMS/lib/python3.6/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/ubuntu/Projects/FMS/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/ubuntu/Projects/FMS/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/home/ubuntu/Projects/FMS/lib/python3.6/site-packages/flask/app.py", line 1615, in full_dispatch_request
return self.finalize_request(rv)
File "/home/ubuntu/Projects/FMS/lib/python3.6/site-packages/flask/app.py", line 1632, in finalize_request
response = self.process_response(response)
File "/home/ubuntu/Projects/FMS/lib/python3.6/site-packages/flask/app.py", line 1858, in process_response
self.save_session(ctx.session, response)
File "/home/ubuntu/Projects/FMS/lib/python3.6/site-packages/flask/app.py", line 924, in save_session
return self.session_interface.save_session(self, session, response)
File "/home/ubuntu/Projects/FMS/lib/python3.6/site-packages/flask/sessions.py", line 363, in save_session
val = self.get_signing_serializer(app).dumps(dict(session))
File "/home/ubuntu/Projects/FMS/lib/python3.6/site-packages/itsdangerous/serializer.py", line 166, in dumps
payload = want_bytes(self.dump_payload(obj))
File "/home/ubuntu/Projects/FMS/lib/python3.6/site-packages/itsdangerous/url_safe.py", line 42, in dump_payload
json = super(URLSafeSerializerMixin, self).dump_payload(obj)
File "/home/ubuntu/Projects/FMS/lib/python3.6/site-packages/itsdangerous/serializer.py", line 133, in dump_payload
return want_bytes(self.serializer.dumps(obj, **self.serializer_kwargs))
File "/home/ubuntu/Projects/FMS/lib/python3.6/site-packages/flask/sessions.py", line 85, in dumps
return json.dumps(_tag(value), separators=(',', ':'))
File "/home/ubuntu/Projects/FMS/lib/python3.6/site-packages/flask/json.py", line 167, in dumps
rv = _json.dumps(obj, **kwargs)
File "/usr/lib/python3.6/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/usr/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/home/ubuntu/Projects/FMS/lib/python3.6/site-packages/flask/json.py", line 81, in default
return _json.JSONEncoder.default(self, o)
File "/usr/lib/python3.6/json/encoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'FileNotFoundError' is not JSON serializable
Please help. Thank you.
Answers
I solved the issue by updating the app.config['UPLOAD_FOLDER_IMG_VEHICLE']. I use os.getcwd() to get the full current path and add the path where the image should be save.
First I check what is the root of the error by adding try and except FileNotFoundError:.
#vehicle_info_form
@app.route('/vehicle_info_form/', methods=['GET', 'POST'])
def vehicle_info_form():
try:
if request.method == "GET":
return render_template(
'vehicleInfo.html'
)
elif request.method == "POST":
inputVehicleImg = request.files['changeVehicleImg']
if inputVehicleImg.filename == "":
inputVehicleImg_filename = ""
else:
print(inputVehicleImg.filename)
print(app.config['UPLOAD_FOLDER_IMG_VEHICLE'])
inputVehicleImg_filename = secure_filename(inputVehicleImg.filename)
try:
inputVehicleImg.save(os.path.join(app.config['UPLOAD_FOLDER_IMG_VEHICLE'], inputVehicleImg_filename))
except FileNotFoundError:
print("File does not exist")
print ('success')
except Exception as e:
print(e)
return redirect(url_for('vehicle_info_form'))
I try to upload an image and return in the console File does not exist.
Then I check the path of app.config['UPLOAD_FOLDER_IMG_VEHICLE'] where the image should be uploaded.
print(app.config['UPLOAD_FOLDER_IMG_VEHICLE'])
And there I found out that the path is not the same in my online server so I update it.
import os
path = os.getcwd()
print(path)
UPLOAD_FOLDER_IMG_VEHICLE = path + "/apps/FMS/static/images/vehicleInfo"
app.config['UPLOAD_FOLDER_IMG_VEHICLE'] = UPLOAD_FOLDER_IMG_VEHICLE
更多推荐

所有评论(0)