Link to home
Start Free TrialLog in
Avatar of klt13
klt13Flag for United States of America

asked on

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.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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

ASKER CERTIFIED SOLUTION
Avatar of klt13
klt13
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of klt13

ASKER

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.....