oskar_15
oskar_15

Reputation: 49

flask file upload not working

I have problem with file upload. I getting no errors but i can't to find file. I was looking for solution but without success. Can anyone help to me? My code:

UPLOAD_FOLDER = '/upload/'
ALLOWED_EXTENSIONS = set(['.xlsx', '.xls'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

def allowed_file(filename):
  print(filename)
  return '.' in filename and \
       filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/file_upload', methods=['GET', 'POST'])
def file():
    if request.method == 'POST':
        print(os.getcwd())
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        # if user does not select file, browser also
        # submit a empty part without filename
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            print(filename)
            filename = secure_filename(file.filename)
            print(filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return redirect(url_for('uploaded_file', filename=filename))

    return render_template("/file_upload.html", role="ok")

this is example form Flask Doc

Upvotes: 0

Views: 2645

Answers (1)

jmetz
jmetz

Reputation: 12773

Your allowed_extensions function is splitting on ".", i.e. returning sub-strings split by ".", but then ALLOWED_EXTENSIONS still contains "."s.

Replace your ALLOWED_EXTENSIONS with

ALLOWED_EXTENSIONS = set(['xlsx', 'xls'])

Upvotes: 1

Related Questions