首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将SPFile保存到本地硬盘

将SPFile保存到本地硬盘
EN

Stack Overflow用户
提问于 2011-03-11 07:21:28
回答 2查看 14.4K关注 0票数 5

我正在尝试从sharepoint检索文件到本地硬盘。

这是我的代码:

代码语言:javascript
复制
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();

当我试图打开该文件时,它将显示为一个损坏的文件。

原始文件大小为186 is,下载后文件大小为191 is。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-11 09:09:48

您不需要BinaryWriter:

代码语言:javascript
复制
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 bytesRead;
    while((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
    {
      fs.Write(buffer, 0, bytesRead);
    }
  }
}
票数 5
EN

Stack Overflow用户

发布于 2011-06-08 14:48:27

只是对这个答案的一点贡献。最好检查每个区块中读取的字节数,以便写入的流将是从SharePoint读取的流的确切副本。见下面的更正。

代码语言:javascript
复制
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);
   }
 }
}
票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5270104

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档