我正在使用带有C#的VS2010开发SharePoint 2010应用程序,我需要一个代码将文件从共享文档复制到本地文件夹。
我写了这段代码:
var dest = @"C:\location";
var source = @"http://server1/sites/PrDB/";
var fileName = "sql.txt";
using (SPSite site = new SPSite(source))
{
using (SPWeb web = site.OpenWeb())
{
SPFile file = web.GetFile("Shared%20Documents/" + fileName);
byte[] b = file.OpenBinary();
FileStream fs = new FileStream(dest + "\\" + file.Name, FileMode.Create, FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(b);
bw.Close();
}
}
我收到了一行错误:file.OpenBinary();
无法打开文件“Shared Documents / sql.txt”。
有人知道为什么吗?我该如何解决?
答案 0 :(得分:0)
要获取文件,您需要传递完整的URL。请尝试以下代码:
var dest = @"C:\location";
var source = @"http://server1/sites/PrDB/";
var fileName = "sql.txt";
using (SPSite site = new SPSite(source))
{
using (SPWeb web = site.OpenWeb())
{
SPFile file = web.GetFile(web.Url +"/Shared%20Documents/" + fileName);// here, this added web url
if(file.Exists)
{
byte[] b = file.OpenBinary();
FileStream fs = new FileStream(dest + "\\" + file.Name, FileMode.Create, FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(b);
bw.Close();
}
}
}
答案 1 :(得分:0)
我知道您正在寻找基于代码的解决方案。但如果您愿意选择OOTB解决方案,那么只需将一行代码附加到锚标记的href
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Game Over" message:@"Your time is up" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *close = [UIAlertAction actionWithTitle:@"close" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
**exit(0);**
}];
UIAlertAction *playagain = [UIAlertAction actionWithTitle:@"Play again" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
[self viewDidLoad];
}];
[alert addAction:close];
[alert addAction:playagain];
[self presentViewController:alert animated:YES completion:nil];
答案 2 :(得分:0)
我在尝试数小时后找到了解决方案,问题在于使用SPWeb读取文件:
var dest = @"C:\location";
var source = @"http://server1/sites/PrDB/";
using (SPSite site = new SPSite(source))
{
using (SPWeb web = site.OpenWeb())
{
SPFolder myfolder = web.GetFolder("Shared Documents");
SPFile file = myfolder.Files[fileName];
byte[] b = file.OpenBinary();
string fullPath =destination + "\\" + file.Name;
FileStream fs = new FileStream(fullPath, FileMode.Create, FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(b);
bw.Close();
}
}