SPAttachmentCollection.AddNow 方法

命名空间:  Microsoft.SharePoint
程序集:  Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)

语法

声明
Public Function AddNow ( _
    leafName As String, _
    data As Byte() _
) As String
用法
Dim instance As SPAttachmentCollection
Dim leafName As String
Dim data As Byte()
Dim returnValue As String

returnValue = instance.AddNow(leafName, _
    data)
public string AddNow(
    string leafName,
    byte[] data
)

参数

  • leafName
    类型:System.String

    一个字符串,指定要附加的文件的名称。

  • data
    类型:[]

    一个字节数组,包含要附加的文件。

返回值

类型:System.String
一个字符串,包含附件的 URL。

备注

如果您通过AddNow方法添加附件,所做的更改将会在数据库中立即生效,并且不需要更新列表项,以便刷新版本信息。但是,附件立即未添加到当前的附件集合对象,并且必须调用获取更新的附件集合的列表项的Attachments属性。

示例

下面的代码示例演示如何在通知列表中添加项的文件附件。

由于AddNow方法需要以二进制格式作为参数传递该文件,该示例使用SPFile类的OpenBinary方法以二进制格式打开该文件夹中的每个文件。

SPSite oSiteCollection = SPContext.Current.Site;
using (SPWeb oWebsite = oSiteCollection.AllWebs["Site_Name"])
{
    SPFile oFile = oWebsite.Folders["Folder_Name"].Files["File_Name"];

    SPList oList = oWebsite.Lists["Announcements"];
    SPListItemCollection collItem = oList.Items;
    SPListItem oListItem = collItem[5];

    SPAttachmentCollection collAttachments = 
         oListItem.Attachments;

    string strFileName = oFile.Name;

    byte[] binFile = oFile.OpenBinary();

    collAttachments.AddNow(strFileName, binFile);
}
Dim oSiteCollection As SPSite = SPContext.Current.Site
Using oWebsite As SPWeb = oSiteCollection.AllWebs("Site_Name")
    Dim oFile As SPFile = oWebsite.Folders("Folder_Name").Files("File_Name")

    Dim oList As SPList = oWebsite.Lists("Announcements")
    Dim collItem As SPListItemCollection = oList.Items
    Dim oListItem As SPListItem = collItem(5)

    Dim collAttachments As SPAttachmentCollection = oListItem.Attachments

    Dim strFileName As String = oFile.Name

    Dim binFile() As Byte = oFile.OpenBinary()

    collAttachments.AddNow(strFileName, binFile)
End Using

备注

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects.

另请参阅

引用

SPAttachmentCollection 类

SPAttachmentCollection 成员

Microsoft.SharePoint 命名空间