Vb.net 下载附件。无法强制转换类型为';System.String';输入';字节[]和#x27;
我试图在VB.NET中下载附件,但出现如下错误: 无法将“System.String”类型的对象强制转换为“System.Byte[]”类型 我的代码:Vb.net 下载附件。无法强制转换类型为';System.String';输入';字节[]和#x27;,vb.net,Vb.net,我试图在VB.NET中下载附件,但出现如下错误: 无法将“System.String”类型的对象强制转换为“System.Byte[]”类型 我的代码: Protected Sub DownloadFile(ByVal sender As Object, ByVal e As EventArgs) Dim id As Integer = Integer.Parse(TryCast(sender, LinkButton).CommandArgument) Dim bytes As
Protected Sub DownloadFile(ByVal sender As Object, ByVal e As EventArgs)
Dim id As Integer = Integer.Parse(TryCast(sender, LinkButton).CommandArgument)
Dim bytes As Byte()
Dim fileName As String, contentType As String
strQry = "select file_name, license_doc, file_type from Driver_Mas where Id=" & Val(id)
Reader = Osql.ExecuteRead(strQry)
While Reader.Read
bytes = DirectCast(Reader.Item("license_doc"), Byte())
contentType = Reader.Item("file_type").ToString()
fileName = Reader.Item("file_name").ToString()
Response.Clear()
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = contentType
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName)
Response.BinaryWrite(bytes)
Response.Flush()
Response.End()
End While
End Sub
您需要使用
Encoding.GetBytes
方法从字符串中获取字节。如有必要,您可以将UTF8更改为任何其他编码
bytes = Encoding.UTF8.GetBytes(Reader.Item("license_doc"))
您需要使用
Encoding.UTF8.GetBytes(“license\u doc”)
从字符串中获取字节。如有必要,您可以将UTF8更改为任何其他编码。他不希望转换字符串“license\u doc”
,而是转换项目“license\u doc”的读卡器内容。因此正确的用法应该是bytes=System.Text.Encoding.UTF8.GetBytes(Reader.Item(“license\u doc”)
@equisde哦,很抱歉错过了它。修正了更新后的答案。谢谢你指出。嗯。。你的编辑有点不正确。。方法是编码。[SomeEncoding]。GetBytes
。编辑正确。方法的名称为。使用[SomeEncoding]
提交您的建议,这是在派生类中重写时使用的。是的,您是对的。[某些编码]属于类编码,因此编码.GetBytes
。使用它的方法是编码。[一些编码].GetBytes()
很抱歉造成混淆(以及误解您的编辑)。