defallowed_file(filename): return'.'in filename and \ filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/upload', methods=['GET', 'POST']) defupload_file(): if request.method == 'POST': # check if the post request has the file part if'file'notin 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', name=filename))
return''' <!doctype html> <title>Upload new File</title> <h1>Upload new File</h1> <form method=post enctype=multipart/form-data> <input type=file name=file> <input type=submit value=Upload> </form> '''
Bash
1 2 3
$ export FLASK_APP=app $ flask run * Running on http://127.0.0.1:5000/
CMD
1 2 3
>set FLASK_APP=app > flask run * Running on http://127.0.0.1:5000/
Powershell
1 2 3
> $env:FLASK_APP = "app" > flask run * Running on http://127.0.0.1:5000/
Then visit http://127.0.0.1:5000/ by chrome, Choose file & Upload . You will find the file occurs in the uploads folder.