共用方式為


SPAttachmentCollection.AddNow method

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'宣告
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
    Type: System.String

    字串,指定要附加的檔案名稱。

  • data
    Type: []

    位元組陣列,包含要附加的檔案。

傳回值

Type: System.String
字串,包含附件的 URL。

備註

如果您將附件新增透過AddNow方法時,變更會立即在資料庫中生效,而且不需要更新的清單項目才能重新整理版本資訊。不過,附件不會立即新增到目前的附件集合物件,而且您必須呼叫清單項目,以取得更新的附件集合中的 [ Attachments ] 屬性。

Examples

下列程式碼範例示範如何在 [佈告] 清單中新增檔案附件的項目。

由於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
注意事項注意事項

某些物件實作IDisposable介面,並且您必須避免之後不再需要保留這些物件在記憶體中。良好的程式碼撰寫方式的相關資訊,請參閱Disposing Objects

請參閱

參照

SPAttachmentCollection class

SPAttachmentCollection members

Microsoft.SharePoint namespace