def img_upload(): if request.method == 'POST': post_data = request.files['photo'] if post_data and allowed_file(post_data.filename): filestream = post_data.read() upload_data = Photo() upload_data.photo = db.Blob(filestream) upload_data.put() comment = "uploaded!" key = upload_data.key() return redirect(url_for('upload')) else: return render_template("img_upload.html")
def album_upload(): form = AlbumUploadForm() albums = Album.query.filter_by(user_id=session.get("user_id")).all() form.album_title.choices = [(item.id, item.title) for item in albums] if len(form.album_title.choices) < 1: flash(message="请先创建一个相册!再上传照片", category='err') return redirect(url_for("album_create")) if request.method == "POST": # 通过 files.getlist() 获得上传的 FileStorage 文件对象列表 fses = request.files.getlist("album_upload") # 检查文件扩展名,将合格的文件过滤出来 valid_fses = check_filestorages_extension(fses, ALLOWED_IMAGE_EXTENSIONS) if len(valid_fses) < 1: flash(message="只允许上传文件类型:" + str(ALLOWED_IMAGE_EXTENSIONS), category='err') return redirect(url_for("album_upload")) else: files_url = [] album_cover = '' # 开始遍历保存每一个合格文件 for fs in valid_fses: album_title = '' for id, title in form.album_title.choices: if id == form.album_title.data: album_title = title folder = session.get("user_name") + '/' + album_title name_orig = secure_filename_with_uuid(fs.filename) fname = photosSet.save(fs, folder=folder, name=name_orig) ts_path = photosSet.config.destination + '/' + folder # 创建并保存缩略图 name_thumb = create_thumbnail(path=ts_path, filename=name_orig, base_width=300) # 创建并保存展示图 name_show = create_show(path=ts_path, filename=name_orig, base_width=800) # 把产生的Photo对象保存到数据库 photo = Photo(origname=name_orig, showname=name_show, thumbname=name_thumb, album_id=form.album_title.data) db.session.add(photo) db.session.commit() # 设置封面文件 album_cover = photo.thumbname # 获取刚才保存的缩略图文件的url furl = photosSet.url(folder + '/' + name_thumb) files_url.append(furl) album = Album.query.filter_by(id=form.album_title.data).first() album.photonum += len(valid_fses) album.cover = album_cover db.session.add(album) db.session.commit() message = "成功保存:" + str(len(valid_fses)) + "张图像; " message += "当前相册共有:" + str(album.photonum) + "张图像" flash(message=message, category='ok') return render_template("album_upload.html", form=form, files_url=files_url) return render_template("album_upload.html", form=form)
def user_album_mine_photo_add(id): album = Album.query.get_or_404(id) if request.method == 'GET': folder = album.user.name + '/' + album.title for photo in album.photos: photo.url = photosSet.url(folder + '/' + photo.thumbname) if request.method == "POST": # 通过 files.getlist() 获得上传的 FileStorage 文件对象列表 fses = request.files.getlist("album_upload") # 检查文件扩展名,将合格的文件过滤出来 valid_fses = check_filestorages_extension(fses, ALLOWED_IMAGE_EXTENSIONS) if len(valid_fses) < 1: flash(message="只允许上传文件类型:" + str(ALLOWED_IMAGE_EXTENSIONS), category='err') return redirect(url_for("user_album_mine_adddel", id=id)) else: # 开始遍历保存每一个合格文件 for fs in valid_fses: folder = session.get("user_name") + '/' + album.title name_orig = secure_filename_with_uuid(fs.filename) fname = photosSet.save(fs, folder=folder, name=name_orig) ts_path = photosSet.config.destination + '/' + folder # 创建并保存缩略图 name_thumb = create_thumbnail(path=ts_path, filename=name_orig, base_width=300) # 创建并保存展示图 name_show = create_show(path=ts_path, filename=name_orig, base_width=800) # 把产生的Photo对象保存到数据库 photo = Photo(origname=name_orig, showname=name_show, thumbname=name_thumb, album_id=album.id) db.session.add(photo) db.session.commit() album.photonum += len(valid_fses) db.session.add(album) db.session.commit() message = "成功保存:" + str(len(valid_fses)) + "张图像; " message += "当前相册共有:" + str(album.photonum) + "张图像" flash(message=message, category='ok') return redirect(url_for("user_album_mine_photo_add", id=id)) return render_template('user_album_mine_adddel.html', album=album, form=PhotoAddForm())
def album_upload(): form = AlbumSelectForm() albums = Album.query.filter_by(user_id=session.get("user_id")).all() form.album_title.choices = [(item.id, item.title) for item in albums] if len(form.album_title.choices) < 1: flash(message="请先创建一个相册!再上传照片", category='err') return redirect(url_for("album_create")) if request.method == "POST": fs_keys = request.files.keys() album_id = int(request.args.get('aid')) for key in fs_keys: fs = request.files.get(key) album_title = '' for id, title in form.album_title.choices: if id == album_id: album_title = title folder = session.get("user_name") + '/' + album_title name_orig = secure_filename_with_uuid(fs.filename) fname = photosSet.save(fs, folder=folder, name=name_orig) ts_path = photosSet.config.destination + '/' + folder # 创建并保存缩略图 name_thumb = create_thumbnail(path=ts_path, filename=name_orig, base_width=300) # 创建并保存展示图 name_show = create_show(path=ts_path, filename=name_orig, base_width=800) # 把产生的Photo对象保存到数据库 photo = Photo(origname=name_orig, showname=name_show, thumbname=name_thumb, album_id=album_id) db.session.add(photo) db.session.commit() # 设置封面文件 album_cover = photo.thumbname # 更新相册的信息 album = Album.query.get_or_404(album_id) album.photonum += 1 album.cover = album_cover db.session.add(album) db.session.commit() # message = "成功保存:" + str(1) + "张图像; " # message += "当前相册共有:" + str(album.photonum) + "张图像" # flash(message=message, category='ok') return redirect(url_for('album_upload')) return render_template("album_upload_dropzone.html", form=form)
def album_upload(): print(current_user) form = AlbumUploadForm() # 根据user_id筛选出相册 items = Album.query.filter_by(user_id=session.get('user_id')) form.album_title.choices = [(item.id, item.title) for item in items] if len(form.album_title.choices) < 1: flash('请先创建相册,再上传图片', 'danger') return redirect(url_for('main.album_create')) print('album_upload:', request.method) if request.method == 'POST': # 同伙getlist方法获取FileStorage文件列表 fs = request.files.getlist('main.album_upload') # 检查文件扩展名,将合格的文件过滤出来 valid_fs = check_filestorage_extension( fs, allowd_extensions=ALLOWED_IMAGE_EXTENSION) if len(valid_fs) < 1: flash('只允许上传类型为:' + str(ALLOWED_IMAGE_EXTENSION), 'danger') return redirect(url_for('main.album_upload')) else: # 开始遍历保存文件 files_url = [] album_cover = '' for fs in valid_fs: print('fs:', fs) # 第四步,使用UploadSet的save方法保存文件 name_org = secure_filename_with_uuid(fs.filename) for id, title in form.album_title.choices: if id == form.album_title.data: album_title = title folder = current_user.name + '/' + album_title fname = photoSet.save(storage=fs, folder=folder, name=name_org) ts_path = photoSet.config.destination + '/' + folder #创建并保存缩略图 name_thumb = create_thumbnail(path=ts_path, filename=name_org, base_width=100) #创建并保存展示图 name_show = create_thumbnail(path=ts_path, filename=name_org, base_width=800) # 把产生的photo对象保存到数据库中 photo = Photo(name_org=name_org, name_show=name_show, name_thumb=name_thumb, album_id=form.album_title.data) db.session.add(photo) db.session.commit() #获取刚才保存的缩略图文件的url fs_url = photoSet.url(folder + '/' + name_thumb) # # 第五步,使用UploadSet的url方法获得文件的url # fs_url = photoSet.url(filename=fname) files_url.append(fs_url) # 设置封面文件 photo.album_cover = photo.name_thumb print('fs_url:', fs_url) album = Album.query.filter_by(id=form.album_title.data).first() album.photonum += len(valid_fs) album.cover = album_cover db.session.add(album) db.session.commit() flash('成功保存了%s张图片' % str(len(valid_fs)), 'success') flash('当前相册共有%s张图片' % str(album.photonum), 'success') return render_template('album_upload.html', files_url=files_url, form=form) return render_template('album_upload.html', form=form)
def album_upload(): # 相册首页 form = AlbumUploadForm() albums = Album.query.filter_by( user_id=session["user_id"]).all() # 获取全部相册标签 # 动态构造form表单数据: 相册的id作为form.album_title.data即 form.album_title.data为当前相册的id form.album_title.choices = [(item.id, item.title) for item in albums] # 获取到数据库中存储的全部相册标签然后动态填写 if len(albums) < 1: flash(message="请先创建一个相册,再上传照片!", category="err") return redirect(url_for("album_create")) if request.method == "POST": fses = request.files.getlist("album_upload") # 获取上传的多个文件 # 检查文件扩展名,将合格的文件过滤出来 valid_fses = check_filestorages_extension( fses, allowed_extensions=ALLOWED_IMAGEEXTENSIONS) if len(valid_fses) < 1: flash(message="只允许上传文件类型:" + str(ALLOWED_IMAGEEXTENSIONS), category="err") return redirect(url_for("album_upload")) else: # 获取当前相册的名称 files_url = [] album_title = "" for id, title in form.album_title.choices: if id == form.album_title.data: album_title = title # 开始遍历保存每一个合格文件 for fs in valid_fses: name_origin = secure_filename_with_uuid(fs.filename) folder = session.get("user_name") + "/" + album_title fname = photosSet.save(fs, folder=folder, name=name_origin) ts_path = photosSet.config.destination + "/" + folder # 创建并保存缩略图 photosSet.config.destination=app.config['UPLOADED_PHOTOS_DEST'] 绝对路径 uploads文件夹 name_thumb = create_thumbnail(path=ts_path, filename=name_origin, base_width=300) # 获取保存缩略图文件的url # furl = photosSet.url(fname) #原图url furl = photosSet.url(folder + "/" + name_thumb) files_url.append(furl) # 创建并保存大图 name_show = create_show(path=ts_path, filename=name_origin, base_width=800) # 将产生的Photo对象保存到数据库中去 photo = Photo(origname=name_origin, showname=name_show, thumbname=name_thumb, album_id=form.album_title.data) # 更新数据库 db.session.add(photo) db.session.commit() furl = photosSet.url(folder + "/" + name_thumb) # files_url.append(furl) # 当前上传图片的相册 print("当前相册id: ", form.album_title.data) # 获取当前相册 form构造的时候 相册的id作为form.album_title.data即 form.album_title.data为当前相册的id album = Album.query.filter_by(id=form.album_title.data).first() album.photonum += len(valid_fses) # 更新数据库 db.session.add(album) db.session.commit() message = "成功上传 " + str( len(valid_fses)) + " 张图片!!" + "当前相册共有 " + str( album.photonum) + " 张图片!!" flash(message=message, category="ok") return render_template("album_upload.html", form=form, files_url=files_url) return render_template("album_upload.html", form=form)