簡體   English   中英

正在下載附件。 無法將類型為“ System.String”的對象轉換為類型為“ System.Byte []”的對象

[英]Downloading an attachment. Unable to cast object of type 'System.String' to type 'System.Byte[]'

我正在嘗試在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 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"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM