2009-08-26 96 views
6

我有以下代碼:SharePoint:如何以編程方式將附件添加到列表項目?

SPList list = web.Lists[this.ListName]; 
    SPListItem item = list.Items.Add(); 

現在我想要做的是:

FileInfo[] attachments = attachmentDirectory.GetFiles(); 
     foreach (FileInfo attachment in attachments) 
     { 
      // Add the attachment from file system to the list item... 

     } 

如何將普通的文件字節數組?

回答

12
foreach (FileInfo attachment in attachments) 
     { 
      FileStream fs = new FileStream(attachment.FullName , FileMode.Open,FileAccess.Read); 

      // Create a byte array of file stream length 
      byte[] ImageData = new byte[fs.Length]; 

      //Read block of bytes from stream into the byte array 
      fs.Read(ImageData,0,System.Convert.ToInt32(fs.Length)); 

      //Close the File Stream 
      fs.Close(); 

      item.Attachments.Add(attachment.Name, ImageData); 


     } 
相關問題