【问题标题】:Error uploading files to SharePoint using office365-rest-api when '%' is in the filename当文件名中包含“%”时,使用 office365-rest-api 将文件上传到 SharePoint 时出错
【发布时间】:2022-01-07 06:03:52
【问题描述】:

我正在使用 office365-rest-api 将文件上传到在线 SharePoint,一切正常,除非我的文件名包含 % 字符。 SharePoint 中生成的文件名中添加了“25”。因此,如果我上传的文件名是“前 50% of Candidates.xls”,那么最终在 SharePoint online 中的文件名是“前 50%25 of Candidates.xls”

我正在上传的代码是:-

ctx=ClientContext('https://mycomp.sharepoint.com/sites/MySite/').with_credentials(userCredentials)
SP_Location="TheLibrary/TheFolder"
fileName="First 50% of Candidates.xls"
path="../../path/First 50% of Candidates.xls"
with open(path, 'rb') as content_file:
 fileContent=content_file.read()
targetFolderUrl=ctx.web.ensure_folder_path(SP_Location).execute_query()
targetFile=targetFolderUrl.upload_file(fileName,fileContent)
ctx.execute_query()

如何设置文件名的格式,以便它能够原封不动地到达 SharePoint?

谢谢

【问题讨论】:

    标签: python sharepoint sharepoint-online


    【解决方案1】:

    % 字符保留在 urls 中。我会使用库对您的文件名进行 URL 编码,或者在发送之前手动将 % 替换为 %25。

    您可以在此处查看对这些字符的引用。 https://www.w3schools.com/tags/ref_urlencode.ASP

    【讨论】:

    • 啊,我明白了。好吧,我尝试用 %25 手动替换 '%' 字符,它在 sharepoint 中显示为 'First 50%25 of Candidates.xls' 我使用了 python string.replace('%','%25') 方法。对吗?
    • 啊,那不行。你能从sharepoint下载文件吗?它是否仍然包含 %,还是仅显示在 URL 中?
    • 进一步查看后,请参阅此帖子。看起来它可能是您的共享点站点上允许这些字符的配置。 techcommunity.microsoft.com/t5/sharepoint-developer/…
    • 是的,我可以下载该文件,它仍然以 50%25 命名,而不仅仅是 50%。我可以进入 SharePoint 并将文件名编辑为 50% 并且工作正常,因此网站允许这样做,并且我已经确认我们的租户确实允许这些特殊字符。所以它只是在某处的 API 上传中迷路了。
    猜你喜欢
    • 2020-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    相关资源
    最近更新 更多