简体   繁体   English

Flask - Firebase 存储 - 图片上传

[英]Flask - Firebase Storage - Image upload

Im trying to allow users to upload images to my firebase storage database this way:我试图允许用户以这种方式将图像上传到我的 firebase 存储数据库:

import pyrebase
from flask import *
app = Flask(__name__)

config = {
    #Hidden
}

firebase = pyrebase.initialize_app(config)
storage = firebase.storage()
auth = firebase.auth()

@app.route('/', methods=['GET', 'POST'])
#Login
def basic():
    unsuccessful = 'Please check your credentials'
    successful = 'Login successful'
    if request.method == 'POST':
        email = request.form['name']
        password = request.form['pass']
        try:
            auth.sign_in_with_email_and_password(email, password)
            return render_template('index.html', s=successful)
        except:
            return render_template('new.html', us=unsuccessful)
    return render_template('new.html')

#Posting function
@app.route('/', methods=['GET','POST'])
def newProduct():
    if request.method == 'POST':
        try:
            path_on_cloud = "images/newproduct.jpg"
            path_local=request.form['file']
            storage.child(path_on_cloud).put(path_local)
            return render_template("index.html")
        except:
            return render_template("index.html")

The basic function which allows the users to log in works without any issues.允许用户登录的基本功能没有任何问题。 However i get the following 404 error when trying to upload a picture to the firebase storage database: "Not Found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again."但是,当我尝试将图片上传到 Firebase 存储数据库时出现以下 404 错误:“未找到服务器上未找到请求的 URL。如果您手动输入了 URL,请检查您的拼写并重试。”

i have two html files: new.html which contains the log in, and index.html which is the page that follows where the upload of an image is done.我有两个 html 文件:new.html 包含登录信息,index.html 是图片上传完成后的页面。

I hope one of you can see what i've done wrong or what im missing.我希望你们中的一个能看到我做错了什么或我错过了什么。 Thanks in advance!提前致谢!

From the code that you shared, you have the same @app.route for both pages.根据您共享的代码,两个页面都具有相同的 @app.route。 Each one should have their own @app.route.每个人都应该有自己的@app.route。

Posting function should be:发布功能应该是:

@app.route('/newProduct', methods=['GET','POST'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM