*-- convert PDF to HEX lcHex = StrToHex(FILETOSTR("c:\temp\SomeFile.pdf")) *-- add to SQL Server SQLEXEC(lnSql, "INSERT INTO YourTable (ImageField) VALUES(" + lcHex + ")")Here's the code for StrToHex (you will need to download Albert Ballinger's FoxCrypto.fll) :
*) Program...........: StrToHex * Author............: Daniel Gramunt * Project...........: common * Created...........: 28.01.2002 - 18:01:04 (Visual FoxPro 07.00.0000.9465) * Copyright.........: (c) Nokia, 2002 *) Description.......: Converts a character string into hexadecimal form. *) : Returns a character string that contains each byte *) : (character) of the passed string in its two-character, *) : hexadecimal form. *) : *) : The returned string is exactly twice as long as < tcString > *) : plus two characters ("0x" prefix to indicate HEX format). *) : *) : Dependencies: This program uses Albert Ballinger's great *) : utility FoxCrypto.fll *) : http://fox.wikis.com/wc.dll?Wiki~FoxCrypto~VFP *) : * Calling Samples...: ?StrToHex("ABC123") && 0x414243313233 * Parameter List....: tcString - Character string that is recognized as a * : byte sequence. * Major change list.: *-------------------------------------------------------------------------------------------------- LPARAMETERS tcString IF NOT "FOXCRYPTO.FLL" $ UPPER(SET("LIBRARY")) SET LIBRARY TO FoxCrypto.fll ADDITIVE ENDIF RETURN "0x" + HexEncoder( tcString, .f. ) *-- EOF StrToHex ----------------------------------------------------------------------------------HTH
> cSQLDataSource = "MyDataSource" > cSQLUserId = "MyUserId" > cSQLPassword = "MyPassword" > > nSQLHandle = SqlConnect (cSQLDataSource, cSQLUserId, cSQLPassword) > > cPdfFile = FileToStr ("The Myth of Self-Describing XML.pdf") > > cSQLString = "Insert Into Teste (nTextField) " + ; > " " + ; > " Values (?cPdfFile) " > > SqlExec (nSQLHandle, cSQLString) >>In order to bypass that problem I adapted Vlad's routine to (FAQ #7995), as you pointed me:
> cSQLDataSource = "MyDataSource" > cSQLUserId = "MyUserId" > cSQLPassword = "MyPassword" > > nSQLHandle = SqlConnect (cSQLDataSource, cSQLUserId, cSQLPassword) > > cPdfFile = FileToStr ("C:\Temp\The horror of XML.doc") > >lcFileName = "TWAIN20.ZIP" && this file got corrupted when use text field on SQL server >* create temporary buffer table > >if file("C:\Temp\Lixo.dbf") > erase ("C:\Temp\Lixo.*") >endif > >create table "C:\Temp\Lixo.dbf" FREE (mData M) >* take binary data from file into memo >use C:\Temp\Lixo.dbf alias ttt >select ttt >append blank >append memo mData from ("C:\Temp\The Myth of Self-Describing XML.pdf") overwrite >use >* change memo field type to general to match to image type on SQL Server >ll = fopen("C:\Temp\Lixo.dbf",12) >fseek(ll,43) >fwrite(ll,'G') >fclose(ll) > >* write data to SQL Server >use C:\Temp\Lixo.dbf alias ttt > > cSQLString = "Insert Into Teste (TestField ) " + ; > " " + ; > " Values (?ttt.mData) " > > ExecuteSql (nSQLHandle, cSQLString, "", .F., Program (), LineNo ()) >use >>Really tricky! After all this solution worked but I don't like having to rely on this kind of workaround.
> PdfFilesDbf = "C:\Temp\PdfFilesDbf" > > If ! File (PdfFilesDbf) > Delete File (PdfFilesDbf + ".*") > endif > > Create Table (PdfFilesDbf) Free (PdfReport M) > Use > > cPdfFile = FileToStr ("C:\Temp\The Myth of Self-Describing XML.pdf") > > Use (PdfFilesDbf) In 0 Alias PdfFilesDbf > > Select PdfFilesDbf > > Insert Into PdfFilesDbf (PdfReport) Value (cPdfFile) > > Use >>Then, latter, if I want to retrieve that information and store it as a .pdf file what is required is just:
> PdfFilesDbf = "C:\Temp\PdfFilesDbf" > > Use (PdfFilesDbf) In 0 Alias PdfFilesDbf > > Select PdfFilesDbf > Go Top > > cMyMemoReport = PdfReport > > StrToFile (cMyMemoReport, "C:\Temp\MyMemoReport.Pdf") > > Use >