有没有一种简单的方法可以通过 python 中的 post 请求接收文件并将其存储在服务器端?

问题描述 投票:0回答:0

我们正在尝试将 .mp3 文件存储在 Python 项目中。我们的前端正在使用 react 和 fetch api。我们现在正在用邮递员测试服务器。我们不断收到对邮递员的错误请求。帖子请求有问题吗?

app.config['ALLOWED_EXTENSIONS'] = ['mp3']
app.config['UPLOAD_FOLDER'] = 'static/'

def post(self):
        file = request.files['file']
        extension = os.path.splitext(file.filename)[1]

        new_song = Song(
            title=request.get_json()['title'],
            likes=0,
            genre=request.get_json()['genre'],
            mp3=os.path.join(
                app.config['UPLOAD_FOLDER'],
                secure_filename(file.filename)
            )
        )

        if file:
            if extension not in app.config['ALLOWED_EXTENSIONS']:
                return 'File is not an mp3.'
            file.save(os.path.join(
                app.config['UPLOAD_FOLDER'],
                secure_filename(file.filename)
            ))

        db.session.add(new_song)
        db.session.commit()
python reactjs flask postman mp3
© www.soinside.com 2019 - 2024. All rights reserved.