我需要帮助将文件从HTML表单直接上载到API。我见过远程URL这样做,但我不知道如何对本地文件这样做?我试着写这个,但不起作用:
uploadmedia = request.files['fileupload']
client = Client('thisismykey')
with open(uploadmedia, 'rb') as file:
new_upload = client.uploads('<space-id>').create(file)
client.uploads
行是此处API文档中指定的行。我只需要能够获得文件路径。
评论意见如下:
# you can use either a file-like object or a path.
# If you use a path, the SDK will open it, create the upload and
# close the file afterwards.
我假设request.files['fileupload']
是一个类似文件的对象,所以我只是传递了它。
上面的代码给了我以下错误:
File "D:Gatsbysubmissionflask-tailwindcss-starterapp__init__.py", line 28, in index
with open(uploadmedia, 'rb') as file:
TypeError: expected str, bytes or os.PathLike object, not FileStorage
我知道在这个例子中,uploadmedia.filename
会得到文件名,但路径的属性是什么?我该怎么做?
request.files['file']
是FileStorage类的一个实例。参考api,不能使用with open(uploadmedia, 'rb') as file:
。
尝试使用流属性:
uploadmedia = request.files['fileupload']
client = Client('thisismykey')
new_upload = client.uploads('<space-id>').create(uploadmedia.stream)