0

I have a form like this

<form id="imageForm" method="post" action="/printDDT">
                <img name=imageFile src={{DDTimage}} width="50%" height="50%">
                <br/><br/>
                <button type="submit">Print DDT</button>
</form>

and I want to read the src in my post function in app.py I tried this

@app.route("/printDDT", methods=["GET", "POST"])
def printDDT():
    if request.method == "POST":
        imageFile = request.files.get('imageFile')
        print(secure_filename[imageFile.filename])
        fileList = updateFile()
        DDTimage = os.path.join(app.config['JPG_FOLDER'], 'ddt.jpg')
        return render_template('home.html', fileList=fileList, DDTimage=DDTimage)

but I got

File "app.py", line 73, in printDDT
    print(secure_filename[imageFile.filename])
AttributeError: 'NoneType' object has no attribute 'filename'

What am I doing wrong?

Edit: In the end I followe this strategy How to get the value of a variable within an html tag in flask

Maybe not the cleanest solution but definitely the smartest.

1 Answers1

0
<form method=post enctype=multipart/form-data>
  <input type=file name=file>
  <input type=submit value=Upload>
</form>

Can you try ?

    @app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        # If the user does not select a file, the browser submits an
        # empty file without a filename.
        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))
            return redirect(url_for('download_file', name=filename))
return """""