C#代码实现上传文件到SharePoint文档库
2008-03-10 18:55
806 查看
SharePoint的文档库是根据数据库虚拟出来的,以HTTP形式呈现,因要创建一个页面单独实现上传功能,故对于其存储和呈现机制进行了学习和研究,不过网络上相关资料还真是很少。SharePoint个人觉得还是比较适合不需要进行复杂逻辑功能的二次开发的网站构建,即适合一般基于office组件的功能门户,能够极大提高效率。
以下代码能够实现往文档库下层目录上传文件的功能,主要还是参考网络上其他文章
using System.IO;
using Microsoft.SharePoint;
using System.Web;
namespace ConsoleApplication1
...{
class Program
...{
static void Main(string[] args)
...{
string fileName = @"C:Documents and SettingsAdministrator桌面word_test.docx";
System.IO.FileInfo myfile = new System.IO.FileInfo(fileName);
byte[] fileContents = new byte[int.Parse(myfile.Length.ToString())];
FileStream fs=File.OpenRead(fileName);
int n = fs.Read(fileContents, 0, int.Parse(myfile.Length.ToString()));
string result = Program.UploadDocument("word_test.docx", fileContents, @"http://zpkxv7t0p3xxqyg:47024/DocLib1/New_Test");
}
public static string UploadDocument(string fileName, byte[] fileContents, string pathFolder)
...{
if (fileContents == null)
...{
return "Null Attachment";
}
try
...{
int iStartIndex = pathFolder.LastIndexOf("/");
string sitePath = pathFolder.Remove(iStartIndex);
string folderName = string.Empty;
if (sitePath.LastIndexOf("/") > 6)
...{
int sec_iStartIndex = sitePath.LastIndexOf("/");
// System.Console.WriteLine(sec_iStartIndex);
sitePath = pathFolder.Remove(sitePath.LastIndexOf("/"));
folderName = pathFolder.Substring(sec_iStartIndex + 1);
// System.Console.WriteLine(folderName);
// System.Console.WriteLine(folderName);
// System.Console.WriteLine(sitePath);
}
// string folderName = pathFolder.Substring(iStartIndex + 1);
// System.Console.WriteLine(folderName);
else
...{
folderName = pathFolder.Substring(iStartIndex + 1);
// System.Console.WriteLine(folderName);
}
SPSite site = new SPSite(sitePath);
SPWeb web = site.OpenWeb();
SPFolder folder = web.GetFolder(folderName);
string fileURL = fileName;
folder.Files.Add(fileURL, fileContents);
if (folder.Files[fileURL].CheckedOutBy.Name != "")
...{
folder.Files[fileURL].CheckIn("File Checked In");
}
return "File added successfully!";
}
catch (System.Exception ex)
...{
return ex.Source + ":" + ex.Message;
}
}
}
}
以下代码能够实现往文档库下层目录上传文件的功能,主要还是参考网络上其他文章
using System.IO;
using Microsoft.SharePoint;
using System.Web;
namespace ConsoleApplication1
...{
class Program
...{
static void Main(string[] args)
...{
string fileName = @"C:Documents and SettingsAdministrator桌面word_test.docx";
System.IO.FileInfo myfile = new System.IO.FileInfo(fileName);
byte[] fileContents = new byte[int.Parse(myfile.Length.ToString())];
FileStream fs=File.OpenRead(fileName);
int n = fs.Read(fileContents, 0, int.Parse(myfile.Length.ToString()));
string result = Program.UploadDocument("word_test.docx", fileContents, @"http://zpkxv7t0p3xxqyg:47024/DocLib1/New_Test");
}
public static string UploadDocument(string fileName, byte[] fileContents, string pathFolder)
...{
if (fileContents == null)
...{
return "Null Attachment";
}
try
...{
int iStartIndex = pathFolder.LastIndexOf("/");
string sitePath = pathFolder.Remove(iStartIndex);
string folderName = string.Empty;
if (sitePath.LastIndexOf("/") > 6)
...{
int sec_iStartIndex = sitePath.LastIndexOf("/");
// System.Console.WriteLine(sec_iStartIndex);
sitePath = pathFolder.Remove(sitePath.LastIndexOf("/"));
folderName = pathFolder.Substring(sec_iStartIndex + 1);
// System.Console.WriteLine(folderName);
// System.Console.WriteLine(folderName);
// System.Console.WriteLine(sitePath);
}
// string folderName = pathFolder.Substring(iStartIndex + 1);
// System.Console.WriteLine(folderName);
else
...{
folderName = pathFolder.Substring(iStartIndex + 1);
// System.Console.WriteLine(folderName);
}
SPSite site = new SPSite(sitePath);
SPWeb web = site.OpenWeb();
SPFolder folder = web.GetFolder(folderName);
string fileURL = fileName;
folder.Files.Add(fileURL, fileContents);
if (folder.Files[fileURL].CheckedOutBy.Name != "")
...{
folder.Files[fileURL].CheckIn("File Checked In");
}
return "File added successfully!";
}
catch (System.Exception ex)
...{
return ex.Source + ":" + ex.Message;
}
}
}
}
相关文章推荐
- C#代码实现上传文件到SharePoint文档库
- C#实现文件上传下载Excel文档示例代码
- C#代码上传文件到SharePoint文档库并给字段赋值
- C#代码上传文件到SharePoint文档库并给字段赋值
- 用WebClinet实现SharePoint上文档库中文件的上传与下载
- ASP.NET(C#)实现一次性动态上传多张图片的代码(多个文件)
- C#实现文件上传及文件下载功能实例代码
- C# 文件上传下载(Excel导入,多线程下载)功能的实现代码
- c#实现按日期命名上传文件代码
- SharePoint下用C#代码上传文档至文档库的子文件夹中
- SharePoint下用C#代码上传文档至文档库的子文件夹中
- ASP.NET(C#)实现一次性动态上传多张图片的代码(多个文件)
- 用WebClinet实现SharePoint上文档库中文件的上传与下载
- 用WebClinet实现SharePoint上文档库中文件的上传与下载
- C#实现按日期命名上传文件代码
- 通过C# 应用程序往SharePoint文档库上传文件
- SharePoint下用C#代码上传文档至文档库的子文件夹中
- C#实现Web文件上传的两种方法实例代码
- ASP.NET(C#)实现一次性动态上传多张图片的代码(多个文件)