如何在BlobStore中设置文件名属性?
问题描述:
我以编程方式上传图片文件并想设置文件名。当我通过POST上传文件时,文件名属性会自动设置。然而,当使用下面的方法时,文件名不会被设置。
I'm programatically uploading image files and want to set the filename. When I upload a file via POST, the filename property is set automatically. However when using the method below, the filename is not getting set.
image = urllib2.urlopen(url)
file_name = files.blobstore.create(mime_type='image/png')
with files.open(file_name, 'a') as f:
f.write(image.read())
files.finalize(file_name)
image_blob_key = files.blobstore.get_blob_key(file_name)
答
从url解析文件名(请参阅相关问题此处 )。然后,您可以通过向files.blobstore.create调用添加一个附加参数来设置它:
Parse the filename from the url (see related question here). Then you can set it by adding an additional parameter to your files.blobstore.create call:
file_name = files.blobstore.create(mime_type='image/png',_blobinfo_uploaded_filename=file_name_from_url)