[Gambas-user] Q:how to save a file.png into Postgresql

Benoit Minisini gambas at ...1...
Sun Aug 31 11:49:59 CEST 2008


On dimanche 31 août 2008, Rolf Schmidt wrote:
> Hi richard
>
> > Public sub Save_Image(ImagePath as string)
> >
> >  Dim img As Image
> >  Dim pictureData As String
> >  Dim sql As String
> >
> >   img = Image.Load(ImagePath)  ' Save temp image as png file
> >   tempFile = Temp() & ".png"
> >   img.Save(tempFile)
> >
> >
> >   pictureData = File.Load(tempFile) 'reload as a string
> >
> >
> > At this point the pictureData string seems  to be ok.
> >
> > I Then tried writing to the database:
> >
> >   sql = "insert into temp_image(piccie)"
> >           "values($$"
> >     sql = sql & pictureData & "$$)"
> >
> > and got back this message:
> >
> > Query failed:ERROR: invalid byte sequence for encoding "UTF8":0x89 HINT:
> > This error can also happen if the byte sequence does not match the
> > encoding expected by the server, which is controlled  by
> > "client_encoding"
>
> Hope you defined your database field as byta for blob.
> Then you have to mask out a lot of byte codes in your picture data.
>
> I did it like this:
>
> PUBLIC SUB SavePic_Click()
>   DIM hFile AS Stream
>   DIM buffer AS Byte
>   DIM picText AS String
>
>   OPEN NameOfPic FOR INPUT AS #hFile
>     WHILE NOT Eof(hFile)
>       READ #hFile, buffer, lof(hFile)
>       IF buffer < 32 OR buffer > 126 THEN
>         picText = picText & "\\\\" & Coct(buffer)
>       ELSE
>         SELECT buffer
>           CASE 92    ' mask  "|" char
>             picText = picText & "\\\\134"
>           CASE 39    ' mask "'" char
>             picText = picText & "\\'"
>           DEFAULT
>             picText = picText & Chr$(buffer)
>         END SELECT
>       END IF
>     WEND
>   CLOSE hFile
>
>   query = "insert into table (picci) values ('" & picText & "')"
>   ....
> END
>
> Hope that helps
> Fine regards
> Rolf

And why doing in Gambas what is already done by the database driver?

-- 
Benoit Minisini




More information about the User mailing list