代码之家  ›  专栏  ›  技术社区  ›  claudiadast

烧瓶文件上载未保存文件

  •  0
  • claudiadast  · 技术社区  · 7 年前

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

    应用程序.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').    
    

    上传.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>
    

    当我上传一个正确的.txt文件并点击“上传”时,它可以工作,但是当我检查目标文件夹时,.txt文件还没有保存在那里。有什么问题吗?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Saige Zhang    7 年前

    你怎么相信你的代码是有效的?你能收到一个成功的消息吗?我只是运行你的代码,做一些修改并成功。

    也许你不知道你的tmp文件夹在哪里?布景怎么样 UPLOAD_FOLDER = "." 所以你可以在 .py 文件的目录。

    这是我的dest文件夹,其中有两个上载的文件:

    E:/tmp

    这是我的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")