ASP / .NET 3.5 & SQL Server 2005
I'm trying to download a blobbed PDF from our SQL database, then without physically writing the file anywhere, output the file and download to user with the "Save / Open".
The code processes through without throwing an Exception, but no download occurs.
To be honest I'm not certain how close this is to "working"... I've tried piecing together several different samples from various posts, being that nothing seems to match my particular situation.
Response.ClearContent()
Response.Clear()
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition", "attachment; filename=MY_DOWNLOAD.pdf")
Dim bufferSize As Integer = 100
Dim outByte(bufferSize - 1) As Byte
Dim strSql As String = "SELECT BlobData, BlobData FROM Attachment WHERE RecordID = " & CInt(Request.QueryString("pid").ToString)
Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("SqlServer").ConnectionString)
connection.Open()
Dim scSql As SqlCommand = New SqlCommand(strSql, connection)
Dim sdrSql As SqlDataReader = scSql.ExecuteReader(CommandBehavior.SequentialAccess)
Do While sdrSql.Read
Dim startIndex As Long = 0
Dim retVal As Long = sdrSql.GetBytes(1, startIndex, outByte, 0, bufferSize)
Do While retVal = bufferSize
Response.BinaryWrite(outByte)
Response.Flush()
startIndex += bufferSize
retVal = sdrSql.GetBytes(1, startIndex, outByte, 0, bufferSize)
Loop
Response.BinaryWrite(outByte)
Response.Flush()
Response.End()
Loop
sdrSql.Close()
End Using