def upload_img():
#http://flask.pocoo.org/docs/0.10/patterns/fileuploads/
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
try:
filename = secure_filename(file.filename)
if len(filename) > 30:
filename = filename[0:30]+'~.'+filename.split('.')[-1]
new_id = get_last_id() + 1
new_filename = filename
new_url = short_url.encode_url(new_id)
img_path = new_url+'.'+filename.split('.')[-1]
file.save(os.path.join(UPLOAD_FOLDER, img_path))
g.db.execute("INSERT INTO pics (id, filename, url, imgpath) VALUES (?, ?, ?, ?)", (new_id, new_filename, new_url, img_path))
g.db.commit()
save_thumbnail(img_path)
#return redirect(url_for('upload_img', filename=filename))
return redirect('/show/'+new_url)
except Exception as e:
return str(e)
else:
return "Wrong file!"
else:
recent = recentlyUploaded()
return render_template('index.html', STATIC_DIR = STATIC_DIR, TEMPLATES_DIR=TEMPLATES_DIR, recent = recent,)
评论列表
文章目录