C# SharePoint:如何以编程方式向列表项添加附件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1334695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SharePoint: How to add an attachment to a list item programatically?
提问by JL.
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?
如何将普通文件转换为字节数组?
采纳答案by JL.
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);
}
回答by swolff1978
Maybe this will point you in a helpful direction:
也许这会为您指明一个有用的方向:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spattachmentcollection.aspx
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
http://msdn.microsoft.com/en-us/library/lists.lists.addattachment.aspx