简体   繁体   English

Flask 文件上传不保存文件

[英]Flask file upload is not saving the file

I've been following a tutorial for a Flask file upload script, which is as follows:我一直在关注一个Flask文件上传脚本的教程,内容如下:

app.py :应用程序.py

from flask import Flask, url_for, render_template, request, flash, redirect
from werkzeug.utils import secure_filename
from datetime import datetime
import json
import subprocess
import os
import sys

UPLOAD_FOLDER = '/tmp/'
ALLOWED_EXTENSIONS = set(['txt'])

app = Flask('author_script')

app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

app.debug = True 

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

@app.route("/", methods=['GET', 'POST'])
def upload():
    if request.method == 'POST':
        if 'file' not in request.files:
            flash("No file part")
            return redirect(request.url)
        file = request.files['file']
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            flash('File uploaded!', 'success')
            proc = subprocess.Popen('python author_script.py {}{} -p n -s n -m num'.format(UPLOAD_FOLDER, file.filename), shell=True, stdout=subprocess.PIPE)
            return redirect(url_for('results'))
    return render_template('upload.html')


# This could redirect the user to the stepfunctions page for their AWS account so they can monitor 
# the pipeline progress.
@app.route('/results')
def results():
    return render_template('results.html').    

upload.html:上传.html:

<!doctype html>
<title>Upload Authors List</title>
<h1>Upload Authors List</h1>
<form action="" method=post enctype=multipart/form-data>
  <p><input type=file name=file>
     <input type=submit value=Upload>
</form>

When I upload a proper .txt file and click "Upload", it works, but then when I check the destination folder, the .txt file hasn't been saved there.当我上传正确的 .txt 文件并单击“上传”时,它可以工作,但是当我检查目标文件夹时,.txt 文件尚未保存在那里。 Any ideas as to what's going wrong?关于出了什么问题的任何想法?

How do you believe that your code works?Can you get a successful message?你怎么相信你的代码有效?你能得到一条成功的消息吗? I just run your code and do some modification and succeed.我只是运行您的代码并进行一些修改并成功。

Maybe you don't know for sure that where your tmp folder is?也许您不确定您的 tmp 文件夹在哪里? How about set UPLOAD_FOLDER = "."设置UPLOAD_FOLDER = "."怎么样? So you can check the file in your .py file 's directory.因此,您可以检查.py文件目录中的文件。

Here is my dest folder with two uploaded files:这是我的 dest 文件夹,其中包含两个上传的文件:

E:/tmp

Here is my python file(E:\\python_project\\test):这是我的 python 文件(E:\\python_project\\test):

from flask import Flask, url_for, render_template, request, flash, redirect
from werkzeug.utils import secure_filename
from datetime import datetime
import json
import subprocess
import os
import sys

UPLOAD_FOLDER = '/tmp/'
ALLOWED_EXTENSIONS = set(['txt','jpg'])

app = Flask('author_script')

app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

app.debug = True

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

@app.route("/", methods=['GET', 'POST'])
def upload():
    if request.method == 'POST':
        if 'file' not in request.files:
            flash("No file part")
            return redirect(request.url)
        file = request.files['file']
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            # flash('File uploaded!', 'success')
            proc = subprocess.Popen('python author_script.py {}{} -p n -s n -m num'.format(UPLOAD_FOLDER, file.filename), shell=True, stdout=subprocess.PIPE)

    return render_template('upload.html')


# This could redirect the user to the stepfunctions page for their AWS account so they can monitor
# the pipeline progress.
@app.route('/results')
def results():
    return render_template('results.html')

app.run("localhost","8080")

Just make sure to add the "."只需确保添加“。” before the path if saving in the current directory.如果保存在当前目录中,则在路径之前。

UPLOAD_FOLDER = './tmp/'

And this shall work!这将起作用!

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

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