I have a binary file in database. How i can make a download of this file in header of page when a button is clicked
Any ide开发者_高级运维as??
Alright, there's a couple of things you might be trying to do, its not completely clear but I'll take a stab at it. Let's say you've got the following basic database comprised of a file identifier and the binary file information:
CREATE TABLE Test
(
FileId int identity(1,1) PRIMARY KEY,
FileData varBinary(max)
)
What you need is a page (in this case called "GetFile.aspx") with the following in the the code-behind's Page_Load event:
'Make sure we've got a querystring
If String.IsNullOrEmpty(Request.QueryString("fileid")) Then
Throw New ApplicationException("Missing querystring parameter FileId")
End If
Dim FileId As Integer
'Make sure the querystring is an Int
If Not Integer.TryParse(Request.QueryString("fileid"), FileId) Then
Throw New ApplicationException("Malformed querystring parameter FileId")
End If
'Change YourDsnHere to point to your database
Using Con As New SqlConnection("YourDsnHere")
Con.Open()
'Select the file
Using Com As New SqlCommand("SELECT * FROM Test WHERE FileId=@FileId", Con)
Com.CommandType = Data.CommandType.Text
Com.Parameters.AddWithValue("@FileId", FileId)
Using RDR = Com.ExecuteReader()
If RDR.Read Then
'Get the data out as a byte array
Dim Bytes() = DirectCast(RDR.Item("FileData"), Byte())
'Clear all response headers
Response.Clear()
'Set the type, my sample is a RAR file, you'll need to look up MIME types for yours
Response.AddHeader("Content-Type", "application/x-rar-compressed")
'Set the name of the file, if you don't set it browsers will use the name of this page which you don't want
Response.AddHeader("Content-Disposition", "inline; filename=YourFilenameHere.rar")
'Optional but nice, set the length so users get a progress bar
Response.AddHeader("Content-Length", Bytes.Count.ToString())
'Push the bytes out raw
Response.BinaryWrite(Bytes)
'Close the response stream so no more HTML gets accidentally pushed out
Response.End()
Else
'Error, no file was found
End If
End Using
End Using
Con.Close()
End Using
Then in your page just when the person clicks the button direct them to this page making sure to pass a valid fileid
in the querystring.
Like I said, I think this is what you're looking for, if not, clarify your description above
Well, if you're generating the file with a script, the most common way a script says it's outputting is to send an HTTP header that looks something like this:
Content-Type: text/html; charset=utf-8
Which will make the browser display as text. In order to force [most browsers] to download the file, you need to send the following additional headers:
Content-Type: application/zip
Content-Disposition: inline; filename=test.zip
Content-Length: 8192
[FILE CONTENTS]
as an example for an 8kb ZIP file named "test.zip". If you can find some way to change the headers sent using the script that generates the file (i.e. pulls it from the database) then it will most likely force the browser to interpret as a file to download.
Then, just link to the script in your header.
精彩评论