CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate: Re: [FishMonger] Binary Data Perl into SQL Binary Field (SQL 2005): Edit Log



cccict
New User

Dec 11, 2015, 9:09 AM


Views: 4341
Re: [FishMonger] Binary Data Perl into SQL Binary Field (SQL 2005)

Hi

Firstly, thank you everyone for your help. We have decided to go with option 6 and recommendations from colleagues here as there was no 'real' reason not to do it that way. Really appreciated.

The appropriate code is below so if it is solved, it may help someone in the future.

use File::Copy;
use DBI;
use CGI;
use Time::Local;

# START : Read in variables from the html page
read(STDIN,$buff,$ENV{'CONTENT_LENGTH'});
# END : Read in variables from the html page

# START : Process $buff in subroutine
# Decodes and splits up variables and stores them into
# $CGI.

&Parse_Multi;

# If code needed for subroutine Parse_Multi, please ask.
# Alternatively to test read in any image file as
# binary from file system.
# END : Process $buff in subroutine

# START : Reference processed data into variable.
# Not necessary but makes easier reading below
$filename = "$CGI{'imageone'}->{'filename'}";
$imgdata = "$CGI{'imageone'}->{'Contents'}";
# END : Reference processed data into variable.
# Not necessary but makes easier reading below

#START: SET UP DATABASE AND CONNECT
#Make sure your DB credentials match here
my $host2 = 'srv-db01\\orsdb';
my $database2 = 'srv_appl';
my $user2 = 'test';
my $auth2 = 'test';

my $dsn2 = "dbi:ODBC:Driver={SQL Server};Server=$host2;Database=$database2";
my $dbh2 = DBI->connect($dsn2, $user2, $auth2, { PrintError => 0 } );

# For info and test table srv_appl.web_news has
# two fields wbn_image1 as var binary max and
# wbn_ref as integer and one record with wbn_ref = 49

#END : SET UP DATABASE AND CONNECT

# START : Set up and run SQL
$sql = "UPDATE web_news SET wbn_image1= CONVERT(varbinary(max),'$imgdata') WHERE wbn_ref='49';";
$sth = $dbh2->prepare($sql);
$sth->execute or die "SQL Error: $DBI::errstr\n";
# END : Set up and run SQL


# In the SQL we simply wanted to store the
# data from $imgdata
# or $CGI{'imageone'}->{'Contents'} into binary SQL field

# As above we have decided not to do this now and using
# alternative where we only store the filename into the
# database table and save the file onto the file system
# For info. code to save file is listed below.

# START : Save the file contents as a file
# onto the file system
$ssh = "\\";
$vdir4="C:\\inetpub\\wwwroot\\storedimages\\";
$prcs = "$$";
$gti1=rindex($filename,"$ssh");
$gti2=length($filename);
$filename=substr($filename,$gti1+1,$gti2-$gti1);
$filename = "i$prcs\_$filename";
open(TMP, ">$vdir4$filename");
binmode(TMP);
print TMP $CGI{'imageone'}->{'Contents'};
close(TMP);
# END : Save the file contents as a file onto the file system

And lastly For info.. HTML code for picking up file below. og_news.cgi is the perl script above.

<form name="NewForm" action="og_news.cgi" method="POST" class="fixed" enctype="multipart/form-data" name="newsform" id="newsform">
<label for="image1">Image One:</label>

<input type="file" name="imageone" id="imageone" accept="image/*"></input>
<input type="submit" value="Submit">
</form>


(This post was edited by cccict on Dec 11, 2015, 9:22 AM)


Edit Log:
Post edited by cccict (New User) on Dec 11, 2015, 9:22 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives