我試圖下載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更改爲任何其他編碼。 –