首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Flask文件上载未保存文件

Flask文件上载未保存文件
EN

Stack Overflow用户
提问于 2018-08-08 02:39:49
回答 2查看 3.6K关注 0票数 1

我一直在关注一个Flask文件上传脚本的教程,如下所示:

app.py

代码语言:javascript
复制
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:

代码语言:javascript
复制
<!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>

当我上传一个合适的.txt文件并点击“上传”时,它可以工作,但是当我检查目标文件夹时,.txt文件并没有保存在那里。你知道哪里出问题了吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-08 03:25:14

你如何相信你的代码可以工作?你能得到一个成功的消息吗?我只是运行你的代码,做了一些修改,并成功了。

也许你不确定你的临时文件夹在哪里?如何设置UPLOAD_FOLDER = ".",以便可以检查.py文件目录中的文件。

这是我的dest文件夹,里面有两个上传的文件:

下面是我的python文件(E:\python_project\test):

代码语言:javascript
复制
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")
票数 5
EN

Stack Overflow用户

发布于 2020-12-05 14:40:07

只需确保添加".“如果保存在当前目录中,则在路径之前。

代码语言:javascript
复制
UPLOAD_FOLDER = './tmp/'

这将会起作用!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51733419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档