SharePoint: How to add an attachment to a list item programatically?

情到浓时终转凉″ 提交于 2019-12-01 03:17:43

问题


I have the following code:

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

now what I want to do is:

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

        }

How do I convert a normal file to a byte array?


回答1:


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


        }



回答2:


Maybe this will point you in a helpful direction:

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spattachmentcollection.aspx

also this might help

http://msdn.microsoft.com/en-us/library/lists.lists.addattachment.aspx



来源:https://stackoverflow.com/questions/1334695/sharepoint-how-to-add-an-attachment-to-a-list-item-programatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!