Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Streaming PDFs back and forth to a db

 

I have a little tool I created in a win form that allows users to view images that are stored in a sql server db as blobs. They pick a name and the blob gets shown. The images are jpegs that I loaded into sql using a different tool that I have.

 

My question is can I do this same procedure with pdf files? Can they be crammed into sql and read back out?

The way I am doing the reading now is like so:

 

byte[] b = (byte [])com.ExecuteScalar();
MemoryStream mem = new MemoryStream(b);
pictureBox1.Image = Image.FromStream(mem);

 

So I query the db and then stream out the image to a picturebox.

What are the limitations of this method? Can it be done on jpeg, .tif, .bmp, pdf?

Wanna-Be C# Superstar
  • Administrators
Posted

You could just store the PDF as a blob in the DB, that way you would be able to store and retreive it using the same code you are using with a jpeg.

Depending on how you want to view the file though will probably require you saving it out as a physical file rather that being able to use a memorystream.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

I think showing the pdf to the user is going to be the tough part. I don't mind having reader pop up if I can get that to work.

Another issue I have is this: I have jpegs and pdfs in the same image datatype field. How can I snoop to figure out which one is coming through the pipe? Or maybe I don't need to because whatever handles the file will try to open it?

Wanna-Be C# Superstar
Posted

The following line of code will pop open the application associated a given file type:

 

System.Diagnostics.Process.Start(imageFilename)

"Never ascribe to malice that which can adequately be explained by incompetence." -- Napolean Bonaparte
  • *Experts*
Posted

From a webserver, you can stream a PDF. You'll have to work out how to convert your blob into a Byte array, but that shouldn't be hard. Then do this:

Response.ContentType = "application/pdf";
byte[] document = GetPDFDocument();
Response.BinaryWrite(document);
Response.End();

 

When the user goes to your aspx page, it will open in Adobe reader, usually within the browser. The user has the option (clientside) to force Adobe to always open by itself (not in a browser). The above code will still work, it just won't open in a browser. That's a user preference, so there's nothing you can do about it.

 

This is how I've streamed PDFs. As with anything, I'm sure there are other ways to do it.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

This is for a win app so I don't think I get response type and stuff like that.

 

I have had a little success using

 

			cc = new SqlConnection(connString);
			com = new SqlCommand(cmd,cc);
			cc.Open();
			byte[] b = (byte [])com.ExecuteScalar();
			MemoryStream mem = new MemoryStream(b);
			string tempPath = Path.GetTempPath();
			tempPath += "\\TempPDF.pdf";
			System.IO.FileStream fs = new System.IO.FileStream(tempPath, FileMode.CreateNew);
			BinaryWriter w = new BinaryWriter(fs);
			w.Write(b);
			w.Close();
			fs.Close();
			this.axPdf1.LoadFile(tempPath);

 

where axPdf1 is the Adobe ActiveX component. The file gets created but then I can't open it in reader (via win explorer) because it says it is either corrupt or not a supported file type.

 

Inside my win app form I get an error about the file not being correct.

Wanna-Be C# Superstar
Posted
This is for a win app so I don't think I get response type and stuff like that.

 

I have had a little success using

 

			cc = new SqlConnection(connString);
			com = new SqlCommand(cmd,cc);
			cc.Open();
			byte[] b = (byte [])com.ExecuteScalar();
			MemoryStream mem = new MemoryStream(b);
			string tempPath = Path.GetTempPath();
			tempPath += "\\TempPDF.pdf";
			System.IO.FileStream fs = new System.IO.FileStream(tempPath, FileMode.CreateNew);
			BinaryWriter w = new BinaryWriter(fs);
			w.Write(b);
			w.Close();
			fs.Close();
			this.axPdf1.LoadFile(tempPath);

 

where axPdf1 is the Adobe ActiveX component. The file gets created but then I can't open it in reader (via win explorer) because it says it is either corrupt or not a supported file type.

 

Inside my win app form I get an error about the file not being correct.

 

I always make it a habit to explicitly call Flush() before closing the stream -- I've had too many corrupted files without doing so...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...