Share via


使用二進位資料 (WCF Data Services)

WCF Data Services 用戶端程式庫可讓您透過下列其中一種方式,從 Open Data Protocol (OData) 摘要擷取及更新二進位資料:

  • 當做實體的基本型別屬性。 如果要處理可以輕鬆載入記憶體的小型二進位資料物件,建議使用這個方法。 在此情況下,二進位屬性是資料模型所公開的實體屬性,而資料服務會將二進位資料序列化成為回應訊息中的 base-64 二進位編碼 XML。

  • 當做個別的二進位資源資料流。 如果要存取及變更可能代表相片、影片或是其他任何類型之二進位編碼資料的二進位大型物件 (BLOB) 資料,建議使用這個方法。

WCF Data Services 會使用 OData 通訊協定中所定義的 HTTP 來實作二進位資料的資料流。 OData 提供下列資料流機制,讓二進位資料與實體產生關聯:

  • 媒體資源/媒體連結項目

    Atom 發行通訊協定 (AtomPub) 會定義一個機制,讓二進位資料當做「媒體資源」(Media Resource) 與資料摘要中的項目 (稱為「媒體連結項目」(Media Link Entry)) 產生關聯。 針對給定的媒體連結項目,可能只能定義一個媒體資源。 媒體資源可以視為實體的預設資料流。 OData 會從 AtomPub 繼承這個資料流行為。

  • 具名的資源資料流

    從 OData 第 3 版開始,一個實體可以有多個透過名稱存取的相關資源資料流。 這個機制與 AtomPub 無關,因此,實體可能會有具名的資源資料流,而不是媒體連結項目。 媒體連結項目也可能會有具名的資料流。 如需詳細資訊,請參閱資料流處理提供者 (WCF Data Services)

實體中繼資料

根據資料流類型,具有相關二進位資源資料流的實體在資料服務中繼資料中,會以下列其中一種方式表示:

  1. 媒體資源:

    以媒體連結項目之實體類型的 HasStream 屬性 (Attribute)。

  2. 具名的資源資料流:

    以屬於 Stream 資料類型之資料模型中某個實體的一個或多個屬性 (Property)。

    對於資料模型中 Stream 資料類型的支援需要 EDM 2.2 版。 如需詳細資訊,請參閱Streaming Provider (WCF Data Services)

在以下的範例中,PhotoInfo 實體是 HasStream 屬性所指出,同時擁有相關媒體資源以及名為 Thumbnail 之具名資源資料流的媒體連結項目。

<EntityType Name="PhotoInfo" m:HasStream="true">
  <Key>
    <PropertyRef Name="PhotoId" />
  </Key>
  <Property Name="PhotoId" Type="Edm.Int32" Nullable="false" 
            p9:StoreGeneratedPattern="Identity" 
            xmlns:p9="https://schemas.microsoft.com/ado/2009/02/edm/annotation" />
  <Property Name="FileName" Type="Edm.String" Nullable="false" />
  <Property Name="FileSize" Type="Edm.Int32" Nullable="true" />
  <Property Name="DateTaken" Type="Edm.DateTime" Nullable="true" />
  <Property Name="TakenBy" Type="Edm.String" Nullable="true" />
  <Property Name="DateAdded" Type="Edm.DateTime" Nullable="false" />
  <Property Name="Exposure" Type="PhotoData.Exposure" Nullable="false" />
  <Property Name="Dimensions" Type="PhotoData.Dimensions" Nullable="false" />
  <Property Name="DateModified" Type="Edm.DateTime" Nullable="false" />
  <Property Name="Comments" Type="Edm.String" Nullable="true" MaxLength="Max" 
            Unicode="true" FixedLength="false" />
  <Property Name="ContentType" Type="Edm.String" Nullable="true" MaxLength="50" 
            Unicode="true" FixedLength="false" />
  <Property Name="Thumbnail" Type="Edm.Stream" Nullable="false" />
</EntityType>

本主題的其餘範例會示範如何存取及變更媒體資源資料流。 如需如何在 .NET Framework 用戶端應用程式中使用 WCF Data Services 用戶端程式庫來使用媒體資源資料流的完整範例,請參閱文章從用戶端存取媒體資源資料流

存取二進位資源資料流

WCF Data Services 用戶端程式庫提供從 OData 架構資料服務存取二進位資源資料流的方法。 下載媒體資源時,您可以使用媒體資源的 URI,或者您可以取得包含媒體資源資料本身的二進位資料流。 您也可以上載媒體資源資料做為二進位資料流。

提示

如需如何建立從存放相片之 OData 服務下載二進位影像檔的 Windows Presentation Foundation (WPF) 用戶端應用程式逐步範例,請參閱文章資料服務資料流處理提供者系列第 2 部分:從用戶端存取媒體資源資料流。若要下載部落格文章中具備之資料流相片資料服務的範例程式碼,請參閱 MSDN Code Gallery 中的資料流處理相片資料服務範例

取得二進位資料流的 URI

擷取特定類型的媒體資源 (例如影像及其他媒體檔案) 時,在應用程式中使用媒體資源的 URI 通常比處理二進位資料的資料流本身更容易。 若要取得與給定媒體連結項目相關聯的資源資料流 URI,您必須在追蹤實體的 DataServiceContext 執行個體上呼叫 GetReadStreamUri(Object) 方法。 若要取得具名資源資料流的 URI,請呼叫採用 name 參數 (資料流的名稱) 的方法多載。 下列範例會示範如何呼叫 GetReadStreamUri(Object) 方法來取得在用戶端上建立新影像所使用的媒體資源資料流 URI:

' Use the ReadStreamUri of the Media Resource for selected PhotoInfo object
' as the URI source of a new bitmap image.
photoImage.Source = New BitmapImage(context.GetReadStreamUri(currentPhoto))
// Use the ReadStreamUri of the Media Resource for selected PhotoInfo object
// as the URI source of a new bitmap image.
photoImage.Source = new BitmapImage(context.GetReadStreamUri(currentPhoto));

針對具名的資源資料流,您也可以直接從資料流屬性取得資料流的 URI。 針對 Stream 類型之資料服務中繼資料所傳回的每個媒體連結項目屬性,請使用資料服務工具產生一個傳回 DataServiceStreamLink 行個體的屬性。 此 DataServiceStreamLinkUri 屬性為具名資源資料流的 URI。 這可讓您直接從媒體連結項目物件或 Language Integrated Query (LINQ) 的結果取得 URI。

下載二進位資源資料流

擷取媒體資源資料流時,您必須在追蹤媒體連結項目的 DataServiceContext 執行個體上呼叫 GetReadStream 方法。 這個方法會將要求傳送到傳回 DataServiceStreamResponse 物件的資料服務,而且此物件擁有包含資源之資料流的參考。 當應用程式需要二進位資源做為 Stream 時,請使用這個方法。 存取具名資源資料流時,請呼叫採用 name 參數 (資源資料流的名稱) 的方法多載。 下列範例會示範如何呼叫 GetReadStream 方法來擷取在用戶端上建立新影像所使用的資料流:

' Get the read stream for the Media Resource of the currently selected 
' entity (Media Link Entry).
Using response As DataServiceStreamResponse = _
        context.GetReadStream(currentEmployee, "image/bmp")

    ' Use the returned binary stream to create a bitmap image that is 
    ' the source of the image control.
    employeeImage.Source = CreateBitmapFromStream(response.Stream)
End Using
// Get the read stream for the Media Resource of the currently selected 
// entity (Media Link Entry).
using (DataServiceStreamResponse response =
    context.GetReadStream(currentEmployee, "image/bmp"))
{
    // Use the returned binary stream to create a bitmap image that is 
    // the source of the image control.
    employeeImage.Source = CreateBitmapFromStream(response.Stream);
}

注意

包含二進位資料流之回應訊息中的 Content-Length 標頭不是由此資料服務所設定。這個值可能不會反映二進位資料流的實際長度。

上載媒體資源做為資料流

若要插入或更新媒體資源,請在追蹤實體的 DataServiceContext 執行個體上呼叫 SetSaveStream 方法。 這個方法會傳送要求給資料服務,此服務中包含從提供的資料流讀取的媒體資源。 上傳具名資源資料流時,請呼叫採用 name 參數 (資源資料流的名稱) 的方法多載。 下列範例示範如何呼叫 SetSaveStream 方法,將屬於特定員工的點陣圖影像傳送給資料服務:

' Set the file stream as the source of binary stream 
' to send to the data service. The Slug header is the file name and
' the content type is determined from the file extension. 
' A value of 'true' means that the stream is closed by the client when 
' the upload is complete.
context.SetSaveStream(photoEntity, imageStream, True, _
    photoEntity.ContentType, photoEntity.FileName)
// Set the file stream as the source of binary stream 
// to send to the data service. The Slug header is the file name and
// the content type is determined from the file extension. 
// A value of 'true' means that the stream is closed by the client when 
// the upload is complete.
context.SetSaveStream(photoEntity, imageStream, true,
    photoEntity.ContentType, photoEntity.FileName);

在這個範例中,SetSaveStream 方法的呼叫方式是為 closeStream 參數提供 true 的值。 如此可確保當二進位資料上載到資料服務之後,DataServiceContext 會關閉資料流。

注意

當您呼叫 SetSaveStream(Object, String, Stream, Boolean, String) 時,並不會將資料流傳送到資料服務,直到呼叫 SaveChanges 為止。

請參閱

概念

將資料繫結至控制項 (WCF Data Services)

其他資源

資料用戶端 (WCF Data Services)