我用它来下载附件,文件正在下载但原始内容不存在,内容只显示为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 = Encoding.UTF8.GetBytes(Reader.Item("license_doc"))
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 :(得分:0)
以下代码从文件名中获取mimetype,coudl要好得多,这应该可以帮到你。
Private Sub ShowDocument(fileName As String, fileContent As Byte())
'Split the string by character . to get file extension type
Dim stringParts As String() = fileName.Split(New Char() {"."C})
Dim strType As String = stringParts(1)
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.AddHeader("content-disposition", Convert.ToString("attachment; filename=") & fileName)
'Set the content type as file extension type
Response.ContentType = strType
'Write the file content
Me.Response.BinaryWrite(fileContent)
Me.Response.End()
End Sub
答案 1 :(得分:0)
你可能想要一些更健壮的东西。这是我在尝试将任意文件发送到浏览器时使用的代码。您应该只需调用WriteFile方法,提供数据。请注意,内容类型取决于您。我们使用“application / octet-stream”强制浏览器只想保存文件,而不是直接在浏览器中打开它。
''' <summary>
''' Writes HTTP headers to support the transfer of a physical data file
''' </summary>
''' <param name="incomingFilename"></param>
''' <param name="fileLength"></param>
''' <param name="response"></param>
''' <param name="contentType"></param>
''' <remarks></remarks>
Public Shared Sub WriteFileResponseHeaders(incomingFilename As String, fileLength As Integer?, response As System.Web.HttpResponse, contentType As String)
For Each c As Char In System.IO.Path.GetInvalidFileNameChars
incomingFilename = incomingFilename.Replace(c, "-"c)
Next
'Strip disallowed characters from the filename
Dim SafeName As String = incomingFilename
Dim BadChars As String = "!@#$%^&*():<>?,/;"""
For i As Integer = 0 To BadChars.Length - 1
SafeName = SafeName.Replace(BadChars.Substring(i, 1), String.Empty)
Next
Dim EncodedUtf As String = "utf-8''"
BadChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$&+=.^_'|~"
For i As Integer = 0 To incomingFilename.Length - 1
If BadChars.IndexOf(incomingFilename.Chars(i)) > -1 Then
EncodedUtf &= incomingFilename.Chars(i)
Else
EncodedUtf &= "%" & AscW(incomingFilename.Chars(i)).ToString("x")
End If
Next
With response
.Clear()
.ClearContent()
.ClearHeaders()
.AppendHeader("content-type", contentType)
.AppendHeader("content-disposition",
String.Format("attachment; filename*={0}; size={1}; creation-date={2}; modification-date={2};read-date={2}; filename=""{3}""",
EncodedUtf, If(fileLength.HasValue, fileLength, 0), Date.Now.ToString("R").Replace(",", " "), SafeName))
End With
End Sub
Public Shared Sub WriteFile(fileData() As Byte, saveAs As String, response As System.Web.HttpResponse, contentType As String)
WriteFileResponseHeaders(saveAs, fileData.Length, response, contentType)
response.OutputStream.Write(fileData, 0, fileData.Length)
response.Flush()
End Sub