我正在尝试将文件上传到我的google云端硬盘。但它没有出现,虽然它已经工作了一段时间,所以我知道铲斗的位置是正确的。但它正在用0b保存它或根本没有它。我该如何解决这个问题?
路线
@app.route('/admin/create/post/<int:id>', methods=['GET', 'POST'])
def create_post(id=None):
"""Display form for creating a post."""
form = forms.Post()
if form.validate_on_submit():
models.Post.create(project_id=id,
name=form.name.data,
content=form.content.data)
post = (models.Post.select()
.where(
(models.Post.name == form.name.data),
(models.Post.content == form.content.data)).get())
for filename in request.files:
file = request.files.get(str(filename))
upload_image_file(file, post.id)
return redirect(url_for('admin'))
return render_template('create_post.html', form=form)
上传图片文件功能
def upload_image_file(filename, filecontent, post_id):
"""Save file."""
logging.debug("""upload_image_file, name={},content={},postid={}"""
.format(filename, filecontent, post_id))
path = 'post/' + '{}/'.format(post_id)
path = path.lower()
filepath = (path + str(filename)).lower()
client = _get_storage_client()
logging.debug('select storage')
bucket = client.bucket(app.config['CLOUD_STORAGE_BUCKET'])
logging.debug('select bucket')
blob = bucket.blob(filepath)
print('file read = {}'.format(filecontent))
# blob.upload_from_string(file.read())
blob.upload_from_string(
'test.txt',
content_type=filecontent)
url = blob.public_url
# try:
# url = url.decode('utf-8')
# logging.debug('decoded url = {}'.format(url))
# except Exception:
# logging.debug('Did not decode = {} | url'.format(url))
models.Media.create(
post_id=post_id,
media=url)
答案 0 :(得分:0)
代码中的错误。主要的一个:
您致电upload_image_file(file, post.id)
然而,upload_image_file()
函数需要3个参数:
def upload_image_file(filename, filecontent, post_id):
你的日志说什么?