2011-10-19 6 views
0

は、私はプログラム的に以下SharePoint 2007:プログラムでバイナリファイルをドキュメントライブラリにアップロードする方法

のSharePoint 2007でドキュメントライブラリにアイテムを作成するために、問題が発生したことは、私のコードの断片である、おそらくあなたかもしれない、私のエラーが何であるかを指摘することができ:

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; 
       } 
      } 
     } 
    } 
}); 

私はヒット "オブジェクト参照がオブジェクトのインスタンスに設定されていません "というコードファイル2.Item.Update();

ありがとうございます。

答えて

1

なぜdestFileをそのように設定しますか?添付ファイルの名前が十分..です

string destFile = file.Name; 

要件は、あなたがフォルダを必要とすることを言う場合には、添付ファイルの相対URLを追加することだけが必要だが、その後、あなたはそれにファイルを追加する前にフォルダを作成したいです。

関連する問題