我试图下载VB.NET的附件,但得到的错误如下:下载附件。无法转换类型“System.String”的对象键入“System.Byte []”
无法转换对象类型 '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 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.UTF8.GetBytes(“license_doc”)'获取从字符串中的字节你可以改变UTF8任何其他编码。如有必要,您可以将UTF8更改为任何其他编码。 –