简体   繁体   English

使用CSOM打开和读取位于SharePoint文档库中的文本文件

[英]Open and Read a text file Located in a SharePoint Document Library using CSOM

I'm able to get the relative path to a file in SharePoint. 我可以在SharePoint中获取文件的相对路径。 I now need to open the file and read its contents. 现在,我需要打开文件并阅读其内容。 This is where I'm stuck. 这就是我卡住的地方。 I don't know how to open a file for reading unless it is local to my hard drive, which it is not. 我不知道如何打开文件进行读取,除非该文件位于我的硬盘驱动器本地(不是)。 Here is my code: 这是我的代码:

If item.FieldValues("File_x0020_Type") = "html" Then
    Dim the_file As SP.File = oWebsite.GetFileByServerRelativeUrl(item.FieldValues("FileRef"))
    Dim reader As StreamReader = New StreamReader(the_file)  
    Dim sr As StreamReader = StreamReader(the_file)
    textbox1.text = sr.ReadToEnd()
    reader.Close()
End If

Sorry misread that. 抱歉,误读了。

ClientContext clientContext = new ClientContext("http://SpSiteUrl");
clientContext.ExecuteQuery();
FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, "thisFile");

System.IO.Stream stream = fileInfo.Stream;

using (StreamReader r = new StreamReader(stream))
     string line;
     while((line = file.ReadLine()) != null)
     {
         System.Console.WriteLine (line);  
     }
}

Once it is set to the Stream, you should be able to loop through it normally. 将其设置为Stream后,您应该可以正常循环通过它。

I also saw this second method. 我也看到了第二种方法。

using (var clientContext = new ClientContext(url))  {
    Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, "thisFile");
    using (var fileStream = System.IO.File.Create(getItem))
     {                  
        fileInfo.Stream.CopyTo(fileStream);
     }
}

Then you would just loop through the fileStream normally as well. 然后,您也可以正常循环遍历fileStream。

Here is a second way to loop as well - 这也是循环的第二种方法-

using (StreamReader sr = new StreamReader(stream)) 
{
      while (sr.Peek() >= 0) 
      {
             Console.WriteLine(sr.ReadLine());
      }
}

And actually, now that I am reading your question one more time - you might be able to do just this. 实际上,既然我再读一次您的问题,您也许可以做到这一点。

Dim reader As StreamReader = New StreamReader(stream)  
textbox1.text = reader.ReadToEnd()
reader.Close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM