pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Issue in save and retreive file in postgres
3+ messages / 2 participants
[nested] [flat]

* Issue in save and retreive file in postgres
@ 2012-06-15 13:16  Vishwas Dwivedi <vishwas.sd@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Vishwas Dwivedi @ 2012-06-15 13:16 UTC (permalink / raw)
  To: pgsql-admin

Greetings,

I am a software programmer and  working on a .net web
application(ASP.NET4.0, c# ).
I am new to postgres and for my current project I am using postgres as
database.


In this need functionality to upload a pdf file  to database and then
retrieve it again from database.
I have taken bytea column in my table to store binary file data. I am
taking byte array to convert the data.

When I try to retrieve this pdf file from database ,its not working.
Please see the below code and suggest me some solution.

Here is my code:
3. ##########   Database table ############

CREATE TABLE testimage
(
  id integer,
  img bytea
)

2. ##########   function to save file in database
protected int UploadDocumentsPostgres()
    {
        int result = 0;
        if (fuDocuments.HasFile)
        {
            fuDocuments.SaveAs(Server.MapPath("Temp/") +
fuDocuments.FileName);

            using (FileStream pgFileStream = new
FileStream(Server.MapPath("Temp/" + fuDocuments.FileName), FileMode.Open,
FileAccess.Read))
            {
                using (BinaryReader pgReader = new BinaryReader(new
BufferedStream(pgFileStream)))
                {
                    byte[] pgByteA =
pgReader.ReadBytes(Convert.ToInt32(pgFileStream.Length));

                    NpgsqlConnection cn = new
NpgsqlConnection("server=195.100.1.100;port=5433;database=DTMS;user
id=postgres;password=admin@123");

                    NpgsqlCommand cmd = new NpgsqlCommand();
                    cmd.Connection = cn;
                    cn.Open();
                    string sql = "insert into testimage values(@id,@data)";

                    cmd.Parameters.Add("@id",
NpgsqlTypes.NpgsqlDbType.Integer, 1).Value = 1;
                    cmd.Parameters.Add("@data",
NpgsqlTypes.NpgsqlDbType.Bytea, pgByteA.Length).Value = pgByteA;

                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();
                    cn.Close();

                }
            }
        }
        return result;
    }


3.    ###################   Retrieve file from database and save in temp
folder ########################
DataTable dt = new DataTable();
        NpgsqlConnection cn = new
NpgsqlConnection("server=195.100.1.100;port=5433;database=DTMS;user
id=postgres;password=admin@123");
        NpgsqlDataAdapter sda = new NpgsqlDataAdapter("select * from
testimage", cn);
        sda.Fill(dt);


        if (dt.Rows.Count > 0)
        {
            byte[] doc = (byte[])(dt.Rows[0]["img"]);
            FileStream fs = new
System.IO.FileStream(Server.MapPath("Temp/") + "temp.PDF", FileMode.Create,
FileAccess.Write);
            BinaryWriter bw = new BinaryWriter(new BufferedStream(fs));

            bw.Write(doc, 0, doc.Length);
            bw.Flush();
            bw.Close();
            fs.Close();
        }
-- 
*Thanks & Regards
Vishwas Dwivedi
Contact No: +91-9460968854*

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Issue in save and retreive file in postgres
@ 2012-06-20 14:39  Craig Ringer <ringerc@ringerc.id.au>
  parent: Vishwas Dwivedi <vishwas.sd@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Craig Ringer @ 2012-06-20 14:39 UTC (permalink / raw)
  To: Vishwas Dwivedi <vishwas.sd@gmail.com>; +Cc: pgsql-admin

On 06/15/2012 09:16 PM, Vishwas Dwivedi wrote:
> Greetings,
>
> I am a software programmer and  working on a .net web 
> application(ASP.NET <http://ASP.NET; 4.0, c# ).
> I am new to postgres and for my current project I am using postgres as 
> database.
>
>
> In this need functionality to upload a pdf file  to database and then 
> retrieve it again from database.
> I have taken bytea column in my table to store binary file data. I am 
> taking byte array to convert the data.
>
> When I try to retrieve this pdf file from database ,its not working.

Error messages?

What happens instead of what you expect to?

After the save, if you examine the data in the database using psql or 
PgAdmin-III, what do you see?

What PostgreSQL version are you using?

--
Craig Ringer

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: [ADMIN] Issue in save and retreive file in postgres
@ 2012-06-23 11:12  Craig Ringer <ringerc@ringerc.id.au>
  parent: Craig Ringer <ringerc@ringerc.id.au>
  0 siblings, 0 replies; 3+ messages in thread

From: Craig Ringer @ 2012-06-23 11:12 UTC (permalink / raw)
  To: Vishwas Dwivedi <vishwas.sd@gmail.com>; pgsql-general@postgresql.org <pgsql-general@postgresql.org>

On 06/23/2012 02:48 PM, Vishwas Dwivedi wrote:
> I am using postgreSQL 9.1.
>
> My problem is when I retrieve pdf file data from postgreSQL and create 
> a pdf
> file in temp folder of my application.
>
> The file gets created but not able to open it, shows some error.
> One more thing that in my code I am converting byte array to file 
> stream when saving and retrieving file from database.
>
> both time save and retrieve, the byte array size differs. I don't know 
> why?

Please reply to the list, not to me.  Unfortunately the list doesn't set 
reply-to.

Please provide the FULL EXACT TEXT OF THE ERROR MESSAGE by copying and 
pasting it into an email.

See: http://wiki.postgresql.org/wiki/Guide_to_reporting_problems


As for byte array differences, which client library are you using? Is it 
possible it's outdated and doesn't understand bytea_format=hex ?  See 
http://www.postgresql.org/docs/9.1/static/datatype-binary.html .

--
Craig Ringer

^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2012-06-23 11:12 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2012-06-15 13:16 Issue in save and retreive file in postgres Vishwas Dwivedi <vishwas.sd@gmail.com>
2012-06-20 14:39 ` Craig Ringer <ringerc@ringerc.id.au>
2012-06-23 11:12   ` Craig Ringer <ringerc@ringerc.id.au>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox