我以前使用的是GetFileByServerRelativeUrl,但工作正常,但是GetFileByServerRelativePath应该支持字符#和%,因此我按如下所示更改了代码,但现在却不起作用带有任何文件?
public bool DownloadFile(string filepath, out string Base64EncodedFile, out string errormessage)
{
Base64EncodedFile = string.Empty;
errormessage = string.Empty;
try
{
Uri filename = new Uri(filepath);
string serverrelative = filename.AbsolutePath;
//This old method does not support # or % but works fine
//Microsoft.SharePoint.Client.File file = context.Web.GetFileByServerRelativeUrl(serverrelative);
// >> Replaced with this
ResourcePath filePathDecoded = ResourcePath.FromDecodedUrl(serverrelative);
Microsoft.SharePoint.Client.File file = context.Web.GetFileByServerRelativePath(filePathDecoded);
// << Replaced with this
context.Load(file);
ClientResult<System.IO.Stream> streamResult = file.OpenBinaryStream();
context.ExecuteQuery();
Base64EncodedFile = ConvertToBase64(streamResult.Value);
return true;
}
catch (Exception ex)
{
errormessage = ex.Message;
return false;
}
}
SharepointClient.SharepointClient newupload = new SharepointClient.SharepointClient("https://xxxxxxx.sharepoint.com/sites/xxxxxxxxx/", usernametext.Text, textpassword.Text);
newupload.DownloadFile(Url.Text, out EncodedAbs, out errormessage);
如果我使用旧的GetFileByServerRelativeUrl可以正常工作...我尝试了所有操作,但似乎无法正常工作GetFileByServerRelativePath ...我不明白自己在做什么错??
请帮助!!!
答案 0 :(得分:2)
我的测试代码供您参考。
{
"took" : 53,
"timed_out" : false,
"_shards" : {
"total" : 10,
"successful" : 10,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 0.7549128,
"hits" : [
{
"_index" : "projecttypeindex",
"_type" : "Projecttype",
"_id" : "5342e739-2019-4021-baf2-55e25b95b8ec",
"_score" : 0.7549128,
"_source" : {
"ProjectID" : "5342e739-2019-4021-baf2-55e25b95b8ec",
"Projectname" : "Test Project1",
"Summary" : "summary of Test Project1",
"Description" : "Description of TestProject1"
}
},
{
"_index" : "componenttypeindex",
"_type" : "Componenttype",
"_id" : "19871386-8065-11e9-bc42-526af7764f64",
"_score" : 0.5565415,
"_source" : {
"ComponentID" : "19871386-8065-11e9-bc42-526af7764f64",
"Componentname" : "some xyz component test",
"Summary" : "this is summary test of test xyz"
}
},
{
"_index" : "componenttypeindex",
"_type" : "Componenttype",
"_id" : "5342e739-1635-4021-baf2-55e25b95b8ec",
"_score" : 0.3164503,
"_source" : {
"ComponentID" : "5342e739-1635-4021-baf2-55e25b95b8ec",
"Componentname" : "TestComponent1",
"Summary" : "this is summary of test component1"
}
},
{
"_index" : "projecttypeindex",
"_type" : "Projecttype",
"_id" : "5342f739-2019-4021-baf2-55e25b95b8ba",
"_score" : 0.2876821,
"_source" : {
"ProjectID" : "5342f739-2019-4021-baf2-55e25b95b8ba",
"Projectname" : "Test Project2",
"Summary" : "summary of Test Project2",
"Description" : "Description of TestProject1"
}
},
{
"_index" : "componenttypeindex",
"_type" : "Componenttype",
"_id" : "90781386-8065-11e9-bc42-526af7764f64",
"_score" : 0.20706992,
"_source" : {
"ComponentID" : "90781386-8065-11e9-bc42-526af7764f64",
"Componentname" : "TestComponent2",
"Summary" : "this is summary of test component3"
}
}
]
}
}
答案 1 :(得分:0)
太好了!谢谢您的示例,我的眼睛睁开了,我发现我的代码唯一的问题是我传递的URL中包含编码的HTML字符!!!我将空格传递为%20,这引起了问题!!!一旦我删除%20并替换为空格,一切都很好!这是有道理的,因为FromDecodedUrl实际上再次对%20进行了编码,导致URL无效!