通过flask上传和保存服务器中的文件会出错

时间:2018-04-09 12:10:53

标签: python python-3.x flask flask-restful

所以,我正在尝试制作一个带有上传按钮的迷你应用程序,该按钮将上传图像,然后将该图像保存在另一个名为“upimgs”的文件夹中。我们需要稍后使用google cloud vision api对上传的图像进行一些图像处理操作。代码是:

app = Flask(__name__)
@app.route('/')
def upload_file():
    return '''<html><h1>Upload a file</h1>
            <body><form action = "http://35.231.238.112:8085/upload" method = "POST" enctype = "multipart/form-data">
            <input type="file" name="file"/><input type="submit"/>
            </form>
            </body></html> '''

@app.route('/upload',methods=["GET","POST"])
def upload_lnk():
    if request.method == 'POST':
            f = request.files['file']
            f.save(os.path.join(app.config['/upimgs/'], filename))
            cmd2 = "python pj2new.py upmigs/" + f.filename
            cmd2 = str(cmd2)
            #some more code to run that output in flask browser

但是它显示以下错误:

KeyError: '/upimgs/'

可能的错误在哪里?我对此表示满意:Refering to a directory in a Flask app doesn't work unless the path is absolute但问题并不相似。我在服务器中上传图像时遇到问题,无论是否存在文件路径问题。

1 个答案:

答案 0 :(得分:0)

将此行添加到您的图片目录

app.config['upimgs'] = "/home/user/app/folder"

Official Docs