c# - 将 SPFile 保存到本地硬盘

标签 c# asp.net sharepoint sharepoint-2010

我正在尝试将文件从共享点检索到本地硬盘。

这是我的代码:

SPFile file = web.GetFile("http://localhost/Basics.pptx");                
byte[] data = file.OpenBinary();
FileStream fs = new FileStream(@"C:\Users\krg\Desktop\xyz.pptx",FileMode.Create,FileAccess.Write);

BinaryWriter w = new BinaryWriter(fs);            
w.Write(data, 0, (int)file.Length);            
w.Close();            
fs.Close();

当我尝试打开该文件时,它显示为已损坏的文件。

The original file size is 186kb, after download the file size is 191kb.

从sharepoint..下载文件的解决方案是什么?

最佳答案

只是对这个答案的一点贡献。最好检查每个 block 中读取的字节数,以便写入的流与从 SharePoint 读取的流完全相同。 请参阅下面的更正。

int size = 10 * 1024;
using (Stream stream = file.OpenBinaryStream())
{  
 using (FileStream fs = new FileStream(@"C:\Users\krg\Desktop\xyz.pptx", FileMode.Create, FileAccess.Write))
 {
   byte[] buffer = new byte[size];
   int nbBytesRead =0;
   while((nbBytesRead=stream.Read(buffer, 0, buffer.Length)) > 0) 
   {
      fs.Write(buffer, 0, nBytesRead);
   }
 }
}

关于c# - 将 SPFile 保存到本地硬盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5270104/

相关文章:

c# - 在 .Net 3.5 中使用动态关键字

c# - 从字节数组中解析短值(蓝牙)

c# - JsonUtility 的包装器如何工作

sql - 从sql存储过程返回多个值

sharepoint - 调试 SharePoint 2007 代码

SharePoint 列表错误 : "Value does not fall within the expected range"

c# - 使用命令行获取进程 ID

c# - LINQ to SQL 业务对象创建最佳实践

c# - SharePoint 使用 API 获取各个网站的大小

javascript - asp.net + 自定义验证器未验证