我正在学习Flask,并且正在尝试完成上传文件。
我得到以下内容[Errno 2]没有带有flask的文件或目录。
如何告诉Flask在正确的位置看?
查看我的 app.html
from flask import render_template, jsonify, Flask, redirect, url_for, request
from app import app
import random
import os
from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
@app.route('/')
@app.route('/index')
def index():
return render_template('index.html', title='Home')
@app.route('/upload.php', methods = ['GET', 'POST'])
def upload():
if request.method == 'POST':
f = request.files['file']
model= ResNet50(weights='imagenet')
img = image.load_img(f.filename)
preds = model.predict(img)
return render_template('uploaded.html', predictions=preds_decoded)
@app.route('/map')
def map():
return render_template('map.html', title='Map')
@app.route('/map/refresh', methods=['POST'])
def map_refresh():
points = [(random.uniform(48.8434100, 48.8634100),
random.uniform(2.3388000, 2.3588000))
for _ in range(random.randint(2, 9))]
return jsonify({'points': points})
@app.route('/contact')
def contact():
return render_template('contact.html', title='Contact')
我在 config.py
中定义了UPLOADED_FOLDER/用户/名称/桌面/ flaskSaaS-master / app / forms
我的 config.py
import logging
from app.config_common import *
from flask import Flask
from flask_debugtoolbar import DebugToolbarExtension
app = Flask(__name__)
# the toolbar is only enabled in debug mode:
app.debug = True
# set a 'SECRET_KEY' to enable the Flask session cookies app.config['SECRET_KEY'] = 'houdini' app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False toolbar = DebugToolbarExtension(app)
# DEBUG can only be set to True in a development environment for security reasons DEBUG = True
# Secret key for generating tokens SECRET_KEY = 'houdini'
# Admin credentials ADMIN_CREDENTIALS = ('admin', 'pa$$word')
# Database choice SQLALCHEMY_DATABASE_URI = 'sqlite:///app.db' SQLALCHEMY_TRACK_MODIFICATIONS = True
# Configuration of a Gmail account for sending mails MAIL_SERVER = 'smtp.googlemail.com' MAIL_PORT = 465 MAIL_USE_TLS = False MAIL_USE_SSL = True MAIL_USERNAME = 'flask.boilerplate' MAIL_PASSWORD
= 'flaskboilerplate123' ADMINS = ['flask.boilerplate@gmail.com']
# Number of times a password is hashed BCRYPT_LOG_ROUNDS = 12
LOG_LEVEL = logging.DEBUG LOG_FILENAME = 'activity.log' LOG_MAXBYTES = 1024 LOG_BACKUPS = 2
UPLOAD_FOLDER = '/Users/name/Desktop/flaskSaaS-master/app/forms'
调试:
* Debugger is active!
* Debugger PIN: 127-013-508
127.0.0.1 - - [29/Sep/2019 20:48:36] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [29/Sep/2019 20:48:36] "GET /_debug_toolbar/static/css/toolbar.css?0.5076085944288159 HTTP/1.1" 200 -
127.0.0.1 - - [29/Sep/2019 20:49:04] "POST /upload.php HTTP/1.1" 500 -
Traceback (most recent call last):
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/flask_debugtoolbar/__init__.py", line 125, in dispatch_request
return view_func(**req.view_args)
File "/anaconda3/envs/tensorflow/lib/python3.6/cProfile.py", line 109, in runcall
return func(*args, **kw)
File "/Users/lorenzocastagno/Desktop/flaskSaaS-master/app/views/main.py", line 27, in upload
img = image.load_img(f.filename)
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/keras_preprocessing/image/utils.py", line 110, in load_img
img = pil_image.open(path)
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/PIL/Image.py", line 2770, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'mmm.jpg'
127.0.0.1 - - [29/Sep/2019 20:49:04] "GET /upload.php?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [29/Sep/2019 20:49:04] "GET /upload.php?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [29/Sep/2019 20:49:04] "GET /upload.php?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [29/Sep/2019 20:49:04] "GET /upload.php?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [29/Sep/2019 20:49:04] "GET /upload.php?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 -
答案 0 :(得分:0)
问题出在img = image.load_img(f.filename)
load_img
将尝试从服务器读取文件。但对于您而言,该文件不存在于服务器的文件系统中,并且可以在request.files
要解决此问题,首先需要将request.files
中的write the file放在某个位置,然后使用该位置加载文件