C# SharePoint 2007:如何以编程方式将二进制文件上载到文档库?
在SharePoint 2007中,以编程方式在文档库中创建项目时遇到问题 下面是我的代码片段,也许您可以指出我的错误:C# SharePoint 2007:如何以编程方式将二进制文件上载到文档库?,c#,asp.net,sharepoint,sharepoint-object-model,C#,Asp.net,Sharepoint,Sharepoint Object Model,在SharePoint 2007中,以编程方式在文档库中创建项目时遇到问题 下面是我的代码片段,也许您可以指出我的错误: SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite(_url)) { using (SPWeb web = site.OpenWeb()) { SPList customList = web.L
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(_url))
{
using (SPWeb web = site.OpenWeb())
{
SPList customList = web.Lists["Test1"];
foreach (SPListItem ltItem in customList.Items)
{
if (ltItem.Attachments != null && ltItem.Attachments.Count > 0)
{
//Get Test1 File Collection
SPFolder folder = web.GetFolder(ltItem.Attachments.UrlPrefix);
SPFileCollection fileColl = folder.Files;
//Get binary data of attachment
SPFile file = ltItem.ParentList.ParentWeb.GetFile(ltItem.Attachments.UrlPrefix + ltItem.Attachments[0]);
byte[] fileData = file.OpenBinary();
//Get Relative URL of attachment destination
string destFile = fileColl.Folder.Url + "/" + file.Name;
web.AllowUnsafeUpdates = true;
//Add attachment into Document Library
SPDocumentLibrary docLib = (SPDocumentLibrary)web.Lists["Test2"];
SPFile file2 = docLib.RootFolder.Files.Add(destFile, fileData, true);
file2.Item.Update();
web.AllowUnsafeUpdates = false;
}
}
}
}
});
我在代码file2.Item.Update()的这个链接中点击了“对象引用未设置为对象的实例”
高级版谢谢。为什么这样设置destFile?附件的名称就足够了
string destFile = file.Name;
如果您的需求表明您需要文件夹,那么只需要附加附件的相对URL,然后在向其中添加文件之前创建文件夹