2015-10-19 82 views
1

我试图下载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 
+0

您需要使用'Encoding.UTF8.GetBytes(“license_doc”)'获取从字符串中的字节你可以改变UTF8任何其他编码。如有必要,您可以将UTF8更改为任何其他编码。 –

回答

2

您需要使用Encoding.GetBytes方法从字符串获取字节。如果必要的话

bytes = Encoding.UTF8.GetBytes(Reader.Item("license_doc")) 
+1

他不想将字符串“license_doc”转换为“license_doc”项目的读取器内容。所以正确的使用应该是'bytes = System.Text.Encoding.UTF8.GetBytes(Reader.Item(“license_doc”))' – equisde

+0

@equisde哦对不起,错过了它。修正了更新后的答案。感谢您指出。 –

+0

umm ..你的编辑是不正确的..方法是'Encoding。[SomeEncoding] .GetBytes'。 –

相关问题