VBAHole22 Posted October 27, 2005 Posted October 27, 2005 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? Quote Wanna-Be C# Superstar
Administrators PlausiblyDamp Posted October 27, 2005 Administrators Posted October 27, 2005 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
VBAHole22 Posted October 27, 2005 Author Posted October 27, 2005 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? Quote Wanna-Be C# Superstar
samsmithnz Posted October 27, 2005 Posted October 27, 2005 Yeh, you'll probably have to Save the pdf in a temp file and then allow the user to open it. Quote Thanks Sam http://www.samsmith.co.nz
VagabondSW Posted October 27, 2005 Posted October 27, 2005 The following line of code will pop open the application associated a given file type: System.Diagnostics.Process.Start(imageFilename) Quote "Never ascribe to malice that which can adequately be explained by incompetence." -- Napolean Bonaparte
*Experts* Nerseus Posted October 27, 2005 *Experts* Posted October 27, 2005 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 Quote "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
VBAHole22 Posted October 27, 2005 Author Posted October 27, 2005 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. Quote Wanna-Be C# Superstar
shaul_ahuva Posted November 4, 2005 Posted November 4, 2005 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... Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.