使用 office365 将文件上传到 Sharepoint 中的子文件夹 API
Upload file to a subfolder in Sharepoint with office365 API
我正在尝试将文件上传到共享点文件夹。我得到了以下代码,它只是将文件上传到根 documents
目录。但我希望它上传到共享点中的特定目录。
变量:
fullurl = 'https://xxxx.sharepoint.com/sites/yyyy/'
fileName = 'report.xlsx'
rootfolder = 'Documents'
targetfolder = '/00 First/01 Second/03 Third/'
所以,文件上传的目标位置是Documents/00 First/01 Second/03 Third/
。
将文件上传到根 documents
文件夹的工作代码。
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file_creation_information import FileCreationInformation
ctx_auth = AuthenticationContext(url=fullurl)
if ctx_auth.acquire_token_for_user(username=username,
password=password):
ctx = ClientContext(fullurl, ctx_auth)
target_list = ctx.web.lists.get_by_title(rootfolder)
info = FileCreationInformation()
with open(fileName, 'rb') as content_file:
info.content = content = content_file.read()
info.url = fileName
info.overwrite = True
upload_file = target_list.rootFolder.files.add(info)
ctx.execute_query()
我尝试更改代码以将文件上传到 targefolder
中给出的子文件夹。
ctx_auth = AuthenticationContext(url=fullurl)
if ctx_auth.acquire_token_for_user(username=username,
password=password):
ctx = ClientContext(fullurl, ctx_auth)
libraryRoot = ctx.web.get_folder_by_server_relative_url(targetfolder)
info = FileCreationInformation()
with open(fileName, 'rb') as content_file:
info.content = content = content_file.read()
info.url = fileName
info.overwrite = True
upload_file = libraryRoot.files.add(info)
ctx.execute_query()
但这失败并以
结束
HTTPError: 400 Client Error: Bad Request for url: https://xxxx.sharepoint.com/sites/yyyy/_api/Web/getFolderByServerRelativeUrl('....')/Files/add(overwrite=true,url='report.xlsx')
ClientRequestException: ('-2147024809, System.ArgumentException', 'Server relative urls must start with SPWeb.ServerRelativeUrl', "400 Client Error: Bad Request for url:
您在代码中设置的文件夹路径似乎不正确。例如,我有一个 SPO 站点 (https://abc.sharepoint.com/sites/s01) 并且该站点有一个默认库 (Shared Documents),然后我想将文件上传到该库中的“FolderA”。
对应的路径应该是:
"/sites/s01/Shared Documents/FolderA"
BR
我正在尝试将文件上传到共享点文件夹。我得到了以下代码,它只是将文件上传到根 documents
目录。但我希望它上传到共享点中的特定目录。
变量:
fullurl = 'https://xxxx.sharepoint.com/sites/yyyy/'
fileName = 'report.xlsx'
rootfolder = 'Documents'
targetfolder = '/00 First/01 Second/03 Third/'
所以,文件上传的目标位置是Documents/00 First/01 Second/03 Third/
。
将文件上传到根 documents
文件夹的工作代码。
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file_creation_information import FileCreationInformation
ctx_auth = AuthenticationContext(url=fullurl)
if ctx_auth.acquire_token_for_user(username=username,
password=password):
ctx = ClientContext(fullurl, ctx_auth)
target_list = ctx.web.lists.get_by_title(rootfolder)
info = FileCreationInformation()
with open(fileName, 'rb') as content_file:
info.content = content = content_file.read()
info.url = fileName
info.overwrite = True
upload_file = target_list.rootFolder.files.add(info)
ctx.execute_query()
我尝试更改代码以将文件上传到 targefolder
中给出的子文件夹。
ctx_auth = AuthenticationContext(url=fullurl)
if ctx_auth.acquire_token_for_user(username=username,
password=password):
ctx = ClientContext(fullurl, ctx_auth)
libraryRoot = ctx.web.get_folder_by_server_relative_url(targetfolder)
info = FileCreationInformation()
with open(fileName, 'rb') as content_file:
info.content = content = content_file.read()
info.url = fileName
info.overwrite = True
upload_file = libraryRoot.files.add(info)
ctx.execute_query()
但这失败并以
结束HTTPError: 400 Client Error: Bad Request for url: https://xxxx.sharepoint.com/sites/yyyy/_api/Web/getFolderByServerRelativeUrl('....')/Files/add(overwrite=true,url='report.xlsx')
ClientRequestException: ('-2147024809, System.ArgumentException', 'Server relative urls must start with SPWeb.ServerRelativeUrl', "400 Client Error: Bad Request for url:
您在代码中设置的文件夹路径似乎不正确。例如,我有一个 SPO 站点 (https://abc.sharepoint.com/sites/s01) 并且该站点有一个默认库 (Shared Documents),然后我想将文件上传到该库中的“FolderA”。
对应的路径应该是:
"/sites/s01/Shared Documents/FolderA"
BR