I have a simple python file that sends a file from a local directory to be displayed in html. And, when the user clicks submit, I want to save this file to a different directory, but I can't seem to make this happen. Here's my code:
Uploader.py
from __future__ import print_function
from random import choice
from flask import Flask, request, redirect, url_for, flash, render_template, abort, send_file, session
from werkzeug.utils import secure_filename
from flask import send_from_directory
import sys, os
app = Flask(__name__)
@app.route('/')
def init(file_Idx=0):
files = os.listdir(DOWNLOAD_FOLDER)
filePath = os.path.join(DOWNLOAD_FOLDER, files[file_Idx])
return render_template('files.html', file=filePath)
@app.route('/upload', methods=['POST'])
def upload_file():
file = request.files['image'] #Failing here!!!
f = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(f)
return render_template('files.html')
files.html
<form action="/upload" method="post" enctype="multipart/form-data">
<img src="{{file}}"/>
<input type=submit name="image">
</form>
Right now the image is displaying, but I can't seem to pass the file to upload_file() to save it in the upload_folder. How can I make this work?