How to download BLOB data from SQL database

I have uploaded some blob data into a sql table. How I have this list displayed in a gridview and working off of the "select" option on the gridview, i want to download the blob data back to my local PC. I use VB.net so i would appreciate that type of code please.

here is my upload code:
        Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("IntranetConnectionString").ConnectionString)
            Dim fn As String
            fn = FileUpload2.FileName.ToString.Trim
            Dim SQL As String
            SQL = "INSERT INTO [Trouble_Ticket_Uploads] ([wo], [filename],[fileobject]) VALUES ('" & Request.QueryString("ID").Trim & "','" & FileUpload2.FileName.ToString.Trim & "', @Data)"
            Dim myCommand As New SqlCommand(SQL, myConnection)
            Dim imageBytes(FileUpload2.PostedFile.InputStream.Length) As Byte
            FileUpload2.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length)
            myCommand.Parameters.AddWithValue("@Data", imageBytes)
            myConnection.Open()
            myCommand.ExecuteNonQuery()
            myConnection.Close()
            Label20.Text = "File uploaded successfully!"
            SqlDataSource7.SelectCommand = "select * from trouble_ticket_uploads where [wo] like '%" & Request.QueryString("ID").Trim & "%'"
            GridView7.DataBind()
            RESOLV.Focus()
        End Using



Now I'm looking for code to get it back out. Thanks in advance.
LVL 1
klt13Asked:
Who is Participating?
 
klt13Author Commented:
I finally figured out how to do it myself in VB.net.  Here is my code in case anyone else may need an example.

  Dim cmd As SqlCommand = New SqlCommand(sql, conn)
        Dim fs As FileStream
        Dim bw As BinaryWriter
        Dim bufferSize As Integer = 300000
        Dim outbyte(300000 - 1) As Byte
        Dim retval As Long
        Dim startIndex As Long = 0
        Dim pub_id As String = ""
        Dim reader As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
        ' Read first record
        reader.Read()


        fs = New FileStream(TextBox2.Text.Trim & "\" & filename, FileMode.OpenOrCreate, FileAccess.Write)


        bw = New BinaryWriter(fs)
        startIndex = 0
        retval = reader.GetBytes(0, 0, outbyte, 0, bufferSize)
        bw.Write(outbyte)
        bw.Flush()
        ' Close the output file.
        bw.Close()
        fs.Close()
        reader.Close()
        ' Clean up connection
        If conn.State = ConnectionState.Open Then
            conn.Close()
            ' Dispose connection
            conn.Dispose()
        End If
0
 
Ray PaseurCommented:
Sorry I do not have an ASP example - this is PHP, but hopefully the headers and the logic about MSIE will be of some use to you.  Best regards, ~ray
<?php // RAY_force_download.php
error_reporting(E_ALL);


// A FILE TO DOWNLOAD - THIS LINK COULD COME IN THE URL VIA $_GET
$url = "http://a0.twimg.com/a/1252097501/images/twitter_logo_header.png";

// USE CASE
force_download($url);


// FUNCTION TO FORCE A DOWNLOAD
function force_download($filename)
{
   $basename = basename($filename);
   $filedata = file_get_contents($filename);

   if ($filedata)
   {
   // THESE HEADERS ARE USED ON ALL BROWSERS
      header("Content-Type: application-x/force-download");
      header("Content-Disposition: attachment; filename=\"$basename\"");
      header("Content-length: ".(string)(strlen($filedata)));
      header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
      header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");

   // THIS HEADER MUST BE OMITTED FOR IE 6+
      if (FALSE === strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE '))
      {
         header("Cache-Control: no-cache, must-revalidate");
      }

   // THIS IS THE LAST HEADER
      header("Pragma: no-cache");

   // FLUSH THE HEADERS TO THE BROWSER
      flush();

   // CAPTURE THE FILE IN THE OUTPUT BUFFERS - WILL BE FLUSHED AT SCRIPT END
      ob_start();
      echo $filedata;
   }
}

Open in new window

0
 
klt13Author Commented:
WARNING!!!!!
Although I posted that I had found a way to download this data, sadly to say, this will download it to the server and NOT the client PC. I thought I was putting it in my C:\temp directory, but it never showed up. I found it in the temp directory located on the server.

Still looking.....
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.