作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
关注 documentation我已经安装了 Flask-Upload。尝试导入时:from flask.ext.uploads import UploadSet, IMAGES
它引发了一个错误:
Traceback (most recent call last):
File "./run.py", line 3, in <module>
from app import app
File "/home/proj/app/__init__.py", line 38, in <module>
from app.admin.views import admin
File "/home/proj/app/admin/views.py", line 14, in <module>
from flask.ext.uploads import UploadSet, Images
ImportError: cannot import name Images
最佳答案
Flask 提供了一个更简单的选项,无需扩展。请参阅以下文档。
http://flask.pocoo.org/docs/0.10/patterns/fileuploads/
这是我遵循链接中的详细信息的实现
进口
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)
@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)
<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>
<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>
关于upload - ImportError : No module named flask. ext.uploads.UploadSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24968242/
我是一名优秀的程序员,十分优秀!