错误:System.Data.Linq.Binary' 无法转换为“Byte 的一维数组”
我正在尝试使用 linq 从数据库返回二进制文件以在浏览器中显示。 下面使用 ado.net 的方法有效,但我尝试升级到 linq,但 linq 版本返回了错误。
Public Sub ProcessRequest(ByVal context As System.Web.HttpContext)
Dim imageId As String = context.Request.QueryString("id")
Dim ret As DataTable = Nothing
ret = DPGetImageData.GetImageById(Convert.ToInt64(imageId))
For Each dt As DataRow In ret.Rows
For Each c As DataColumn In ret.Columns
If Not (dt(c) Is Nothing) Then
context.Response.Clear()
context.Response.BufferOutput = False
context.Response.OutputStream.Write(CType(dt.Table.Rows(0).Item("imageData"), Byte()), 0, CInt(dt.Table.Rows(0).Item("imageSize")))
context.Response.End()
End If
Next
Next
End Sub
工作 Linq 版本:
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
If Not String.IsNullOrEmpty(Current.Request.QueryString("Id")) Then
Dim imageId = HttpContext.Current.Request.QueryString("Id")
Dim result = repository.FindById(Convert.ToInt64(imageId)).imageData.ToArray
HttpContext.Current.Response.BinaryWrite(result)
context.Response.End()
End If
End Sub
I am trying to return a binary from the DB using linq for display in the browser. The method below using ado.net works but I am trying to ypgrade to linq but the linq version returned the error.
Public Sub ProcessRequest(ByVal context As System.Web.HttpContext)
Dim imageId As String = context.Request.QueryString("id")
Dim ret As DataTable = Nothing
ret = DPGetImageData.GetImageById(Convert.ToInt64(imageId))
For Each dt As DataRow In ret.Rows
For Each c As DataColumn In ret.Columns
If Not (dt(c) Is Nothing) Then
context.Response.Clear()
context.Response.BufferOutput = False
context.Response.OutputStream.Write(CType(dt.Table.Rows(0).Item("imageData"), Byte()), 0, CInt(dt.Table.Rows(0).Item("imageSize")))
context.Response.End()
End If
Next
Next
End Sub
Working Linq Version:
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
If Not String.IsNullOrEmpty(Current.Request.QueryString("Id")) Then
Dim imageId = HttpContext.Current.Request.QueryString("Id")
Dim result = repository.FindById(Convert.ToInt64(imageId)).imageData.ToArray
HttpContext.Current.Response.BinaryWrite(result)
context.Response.End()
End If
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该能够只调用 Response.BinaryWrite(result.ToArray())。 注意括号,ToArray 是一个方法调用。
You should be able to just call Response.BinaryWrite(result.ToArray()). Notice the parantheses, ToArray is a method call.
您不需要将其转换为 Binary.ToArray,它已经返回一个字节数组。
另一种方法是直接使用字节数组。 您可以在设计器中对其进行修改。
更新:
我不完全确定,但可能您的 DataContext 已被处置。 我认为 Binary 类型使用延迟加载。
如果您添加堆栈跟踪,我们可以查看情况是否如此。
You should not need the cast as Binary.ToArray, returns a byte array already.
An alternative is to use a byte array directly. You can modify it in the designer.
UPDATE:
I am not fully sure, but it could be that your DataContext has been disposed already. I think the Binary type uses deferred loading.
If you add a stacktrace, we could see if that is the case.