Flask offre une option plus simple sans besoin d'extension. Voir la documentation ci-dessous. http://flask.pocoo.org/docs/0.10/patterns/fileuploads/
Voici ma mise en œuvre suivant les détails dans le lien
Importations
import os
from werkzeug import secure_filename
from flask import send_from_directory
#app config
UPLOAD_FOLDER = os.path.dirname(__file__)
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif','doc','docx','xls'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = os.path.join(UPLOAD_FOLDER,'uploads') # uploads folder should exists in the server
#function that checks the file extension
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
#function that handles the document upload page
@app.route('/input_docs/<contract_id>', methods=['GET', 'POST'])
def input_docs(contract_id):
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], contract_id+'_'+ filename))
else:
flash("Not a valid file %s" % file.filename)
contract = Contracts.query.filter_by(id = contract_id).first()
try:
attachments = [file[file.find('_')+1::] for file in os.listdir(app.config['UPLOAD_FOLDER']) if file.startswith(contract_id+'_')] # Strip out the contract id for from display
except OSError as err:
attachments = []
doc_form = InputDocsForm(obj=contract)
return render_template('input_docs.html',title = 'Input Docs', form=doc_form,attachments=attachments)
fonction
qui gère sélectionnez Télécharger le document
@app.route('/download_docs/<filename>')
def download_docs(filename):
app.logger.info("Downloading %s"% os.path.join(app.config['UPLOAD_FOLDER'], filename))
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
// modèle côté client
<div class="row">
<div class="span3">
<p><input type=file name=file>
</div>
<div class="span3">
<input type=submit value=Upload>
</div>
<div class="span3">
<a href="{{ url_for('contracts_list') }}"> <input class="btn btn-primary" type="button" value="{{ _('Close') }}"></a>
<input id = 'download' class="btn btn-primary" type="button" value="{{ _('Download') }}">
</div>
</div>
<table class="table">
<thead>
<tr>
<th>Select</th>
<th>Attachment</th>
</tr>
</thead>
<tbody>
{% for attachment in attachments %}
<tr>
<td >{{ attachment }}</td>
<td id = "sel" onclick = 'onSelectDoc("{{attachment}}" )'> {{ form.sel (type ="checkbox") }} </td>
</tr>
{% endfor %}
</tbody>
</table>
// côté client javascript
<script>
var Attachments = [];
$(document).ready(function(){
$("#download").click(function() {
if (Attachments.length > 0){
var contract_id = document.getElementById('id').value;
var url = flask_util.url_for('download_docs' ,{ filename:contract_id+"_"+Attachments[0] });
var k = Attachments.length;
while (k--) Attachments.shift();
$(location).attr('href',url);
}
else{
alert("Select at least one attachment to download");
return;
}
});
function onSelectDoc(attachment){
if (attachment.length>0) {
var i = Attachments.indexOf(attachment)
if (i>=0) {
Attachments.splice(i,1) ; //remove the attachment on unselect
return;
}
}
Attachments.push(attachment);
}
</script>
apparemment il y a 2 différentes extensions deux appelé Flask-uploads par différents auteurs. I_ಠ – endolith