简体   繁体   English

Flask,Apache,mod_wsgi:无法在服务器端保存文件

[英]Flask, Apache, mod_wsgi: unable to save file on server side

From the client side I am sending an image via post from form enctype=multipart/form-data , and on the server side I am saving it to a directory. 从客户端,我通过form enctype=multipart/form-data通过发布发送图像,在服务器端,我将其保存到目录中。 All of this works locally on my computer and running flask directly with python app.py . 所有这些都可以在我的计算机上本地运行,并直接通过python app.py运行flask。

Here is my reference for setting up file saving: 这是设置文件保存的参考:

http://flask.pocoo.org/docs/patterns/fileuploads/ http://flask.pocoo.org/docs/patterns/fileuploads/

On the actual production server, I am running it with Apache and mod_wsgi, which I set up according to this website: 在实际的生产服务器上,我使用Apache和mod_wsgi来运行它,我根据此网站对其进行了设置:

http://flask.pocoo.org/docs/deploying/mod_wsgi/ http://flask.pocoo.org/docs/deploying/mod_wsgi/

For directory permissions I have tried chown -R 777 and chown -R www-data:www-data where the relevant Apache code for users looks like this: WSGIDaemonProcess app user=www-data group=www-data threads=5 . 对于目录权限,我尝试使用chown -R 777chown -R www-data:www-data ,其中用户的相关Apache代码如下所示: WSGIDaemonProcess app user=www-data group=www-data threads=5

However, after all of this I am still not able to get the file to save. 但是,毕竟,我仍然无法保存该文件。 I just get a 500 HTTP error back at the point where it tries to save the file. 我只是在尝试保存文件时返回500 HTTP错误。

Here is the relevant Flask code: 以下是相关的Flask代码:

UPLOAD_FOLDER = '/images/'
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

@app.route('/upload_ocr_images', methods=['GET', 'POST'])
def upload_images():
    if request.method == 'POST':
        files = request.files.getlist("images[]")
        for file in files:
            if allowed_file(file.filename):
                filename = secure_filename(file.filename)
                file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
    return redirect(url_for('home'))

At this point I am wondering if there is something I need to be setting on the Apache side of things. 在这一点上,我想知道是否需要在Apache方面进行设置。

Youre using /uploads as your path. 您使用/uploads作为路径。 That means you're trying to upload to a directory named /uploads at root level of your filesystem. 这意味着您要尝试在文件系统的根目录上载到名为/uploads的目录。

This is usually wrong and normally it's the error. 这通常是错误的,通常是错误。

If you've the uploads folder under your flask application file structure, then you should create the path using app.root_path which holds the absolute application path. 如果您 flask应用程序文件结构拥有了uploads文件夹,则应使用app.root_path创建路径,该路径包含绝对应用程序路径。

Something like 就像是

file.save(os.path.join(app.root_path, '/uploads', filename))

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

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