从代码错误“找不到文件”上传文件

时间:2018-03-27 05:02:50

标签: sharepoint sharepoint-online

我将文件从字节数组上传到Sharepoint online dcoument librar并在执行时收到错误“找不到文件”

    public static bool UploadFile(SP sp, string folderName, string fileNameWithExtension, byte[] fileContent, TraceWriter log)
{
    bool result = false;
    try
    {       
        SecureString securePwd = new SecureString();
        char[] pwdarray = sp.pwd.ToCharArray();

        foreach (var item in pwdarray)
        {
            securePwd.AppendChar(item);
        }

        SharePointOnlineCredentials creds = new SharePointOnlineCredentials(sp.id, securePwd);

        using (ClientContext clientContext = new ClientContext(sp.url))
        {
            log.Info("UploadToSharePoint 1");

            clientContext.Credentials = creds;
            clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
            Web web = clientContext.Web;           

            var fileCreationInformation = new FileCreationInformation();
            fileCreationInformation.Content = fileContent;
            fileCreationInformation.Overwrite = true;          

            fileCreationInformation.Url = fileNameWithExtension;            
            Microsoft.SharePoint.Client.List docs = web.Lists.GetByTitle("All Attachments");
            docs.RootFolder.Folders.Add(folderName);
            Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(fileCreationInformation);           
            clientContext.ExecuteQuery();          
            return result = true;    
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

请帮助花了2天但没有找到解决方案:(

1 个答案:

答案 0 :(得分:0)

以下示例代码适用于我。

using (var context = new ClientContext("https://domain.sharepoint.com/sites/Developer"))
            {                 
                Console.ForegroundColor = ConsoleColor.Green;
                string password = "pw";
                SecureString sec_pass = new SecureString();
                Array.ForEach(password.ToArray(), sec_pass.AppendChar);
                sec_pass.MakeReadOnly();
                context.Credentials = new SharePointOnlineCredentials("lee@domain.onmicrosoft.com", sec_pass);

                string fileName = "C:\\Lee\\test.docx";
                FileStream stream = System.IO.File.OpenRead(fileName);
                byte[] fileBytes = new byte[stream.Length];

                stream.Read(fileBytes, 0, fileBytes.Length);
                stream.Close();


                var fileCreationInformation = new FileCreationInformation();
                fileCreationInformation.Content = fileBytes;
                fileCreationInformation.Overwrite = true;
                fileCreationInformation.Url = "test.docx";

                Microsoft.SharePoint.Client.List docs = context.Web.Lists.GetByTitle("MyDoc3");                
                Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(fileCreationInformation);
                context.ExecuteQuery();
                Console.WriteLine("done");
                Console.ReadKey();
            }