2015-10-19 83 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'。 –

相關問題