SPFile.OpenBinary method
開啟檔案以二進位格式。
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Syntax
'宣告
Public Function OpenBinary As Byte()
'用途
Dim instance As SPFile
Dim returnValue As Byte()
returnValue = instance.OpenBinary()
public byte[] OpenBinary()
傳回值
Type: []
位元組陣列,包含檔案內容。
Examples
下列程式碼範例會逐一查看共用文件的文件庫目前的網站中的檔案集合,並以二進位格式開啟每個檔案,以便將它附加至指定的項目,在 [事件] 清單中。OpenBinary方法失敗時,如果檔案的大小為 0 (零) 個位元組。
Dim web As SPWeb = SPContext.Current.Web
Dim attachFolder As SPFolder = web.Folders("Shared Documents")
Dim attachFiles As SPFileCollection = attachFolder.Files
Dim attachList As SPList = web.Lists("Events")
Dim attachItem As SPListItem = attachList.Items(10)
Dim attachments As SPAttachmentCollection = attachItem.Attachments
Dim attachFile As SPFile
For Each attachFile In attachFiles
Dim fileName As String = attachFile.Name
Dim binFile As Byte() = attachFile.OpenBinary()
attachments.Add(fileName, binFile)
Next attachFile
attachItem.Update()
SPWeb oWebsite = SPContext.Current.Web;
SPFolder oFolder = oWebsite.Folders["Shared Documents"];
SPFileCollection collFiles = oFolder.Files;
SPList oList = oWebsite.Lists["Events"];
SPListItem oListItem = oList.Items[10];
SPAttachmentCollection collAttachments = oListItem.Attachments;
foreach (SPFile oFile in collFiles)
{
string strFilename = oFile.Name;
byte[] binFile = oFile.OpenBinary();
collAttachments.Add(strFilename, binFile);
}
oListItem.Update();