一:Fiddler抓包工具的使用
抓图片上传请求:
分别为post单变量与多变量
request.file:
file = request.files['file']
print dir(file)
传一个图片
传多张图片
MultiDict
if file.filename.find('.') > 0:
file_ext = file.filename.rsplit('.', 1)[1].strip().lower()
rsplit() 方法语法:
1
|
S.rsplit([sep
=
None
][,count
=
S.count(sep)])
|
def save_to_local(file, file_name): save_dir = app.config['UPLOAD_DIR'] file.save(os.path.join(save_dir, file_name)) #合并目录
# os.path.join('/hello/','good/boy/','doiido')
request常用属性:
https://www.cnblogs.com/wangjikun/p/6935592.html
三.云存储
本次使用qiniu云,因为注册认证后会给10g的免费额度。
接下来首先看看官方的SDK-python
1.在pycharm里install qiniu
2.
app.conf
ALLOWED_EXT=set(['png', 'jpg','jpeg','bmp','gif']) UPLOAD_DIR='D:/upload/' QINIU_ACCESS_KEY='aWbnpU2phR63jQJjEwmRmIjPWP0U8h3sho6OW0BA' QINIU_SECRET_KEY='siL6VE2UCus1_Xl601nOLRA8ax4n4O8bi9iLqZo8' QINIU_BUCKET_NAME='nowstagramimage' QINIU_DOMAIN='http://p4n08apag.bkt.clouddn.com/'qiniusdk.py
# -*- coding: utf-8 -*- from nowstagram import app from qiniu import Auth, put_stream, put_data import os access_key = app.config['QINIU_ACCESS_KEY'] secret_key = app.config['QINIU_SECRET_KEY'] # 构建鉴权对象 q = Auth(access_key, secret_key) # 要上传的空间 bucket_name = app.config['QINIU_BUCKET_NAME'] domain_prefix = app.config['QINIU_DOMAIN'] def qiniu_upload_file(source_file, save_file_name): # 上传到七牛后保存的文件名 # key = 'my-python-logo.png'; # 生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, save_file_name) ret, info = put_data(token, save_file_name, source_file.stream) print(info) if info.status_code == 200: return domain_prefix + save_file_name return None
#view.py @app.route('/upload/', methods={"post"}) def upload(): file = request.files['file'] #request的files属性为请求中文件的数据 if file.filename.find('.') > 0: file_ext = file.filename.rsplit('.',1)[1].strip().lower() if file_ext in app.config['ALLOWED_EXT']: file_name = str(uuid.uuid1()).replace('-','')+'.'+file_ext #不用用户的图片名字,自己用uuid来产生一个新的名字 #url = save_to_local(file, file_name) #使用qiniu url = qiniu_upload_file(file, file_name) if url != None: db.session.add(Image(url, current_user.id)) db.session.commit() return redirect('/profile/%d' % current_user.id)
可以进行云实时缩图:
class="img-box">
src="{{ image.url }}?imageView/1/w/290/h/290">