【问题标题】:How to read binary data/image from WCF REST如何从 WCF REST 读取二进制数据/图像
【发布时间】:2011-03-12 12:40:17
【问题描述】:

我有以下几点:

公共接口 INorthwindSvc

<OperationContract()>
<WebGet(UriTemplate:="EmployeePictureBytes?id={EmpId}")>
Function GetEmployeePictureBytesById(ByVal EmpId As String) As Byte()

结束界面

我实现的方法(使用 EF 4.0)如下:

Public Function GetEmployeePictureBytesById(ByVal EmpId As String) As Byte() Implements INorthwindSvc.GetEmployeePictureBytesById
    Dim ctxt As New NorthwindEntities
    Dim q = From c In ctxt.Employees
            Where c.EmployeeID = EmpId
            Select c.Photo

    Return q.FirstOrDefault
End Function

当我从浏览器访问操作时,我能够接收字节。如果我尝试使用 Win Client 访问相同的内容,如下所示(发生错误,如内联所示):

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim o As New WebClient
    Dim b() As Byte = o.DownloadData(New Uri("http://localhost:8732/WcfWebHttpSvcLib/rest/EmployeePictureBytes?id=2"))

    Dim ms As New MemoryStream()
    ms.Write(b, 0, b.Length)

    Dim original As New System.Drawing.Bitmap(ms) 'error: parameter is not valid

End Sub

我也尝试过使用 Image.FromStream。但是,仍然没有运气。

谁能帮我解决这个问题?

谢谢

【问题讨论】:

    标签: wcf wcf-rest


    【解决方案1】:

    写入内存流后,您不会倒带,因此尝试从中读取会失败也就是说,即使这样也没有必要,因为您可以写:

    im ms As New MemoryStream(b)
    ' now call FromStream
    

    【讨论】:

      猜你喜欢
      • 2010-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-21
      • 2018-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多