Downloading pdf files from an sql server


(Jan Strid) #1

Hi everybody,
I am absolutely in the dark here, so I need directions. I have pdf files stored on an sql server as varbinary[ ], and I want to download one and display it in a browser window using my code behind. I assume it is basically handled through a normal “Select” sqlCommand and interpreted by some datareader. But to be honest, I can’t even figure out what datatype the receiving variable should have, not to speak of how I would make the browser recognize and present it as a pdf. Neither do I know if this question is put under the appropriate category, by the way. As I’m sure you have figured out - this is by no means my profession, although at this time this particular issue is important to me. Maybe there are suitable tutorials, code samples or similar available somewhere. Any hints appreciated.
J


(antonk) #2

Hello

The question is way too broad to be answered properly. You didn’t specify even the server platform you want to use.
The simplest thing Remoting SDK can do for you is the following:

1.Create new Console application
2.Reference assemblies Remoting.SDK and Remoting.SDK.Server
3.Add a new file named licenses.licx and set its Build Action to Embedded Resource
4. Put this line into the file:

RemObjects.SDK.Server.IpHttpServerChannel, RemObjects.SDK.Server

5.Put this code to Program.cs:

class Program
{
	static void Main(string[] args)
	{
		var serverChannel = new IpHttpServerChannel();
		serverChannel.Port = 8099;

		var dispatcher = new HttpFileDispatcher();
		dispatcher.Path = "/pdf";
		dispatcher.Request += Dispatcher_Request;
		dispatcher.Server = serverChannel;

		serverChannel.Open();

		Console.WriteLine("Press ENTER to exit");
		Console.ReadLine();
	}

	private static void Dispatcher_Request(object sender, RequestEventArgs e)
	{
		// Parse path here and read the corresponding PDF file contents
		// then put that content into e.Data

		e.Path = "file.pdf";
		e.Data = new MemoryStream(File.ReadAllBytes(...here should be a path to a sample PDF file...));
	}
}

6.Start application and open the following URL in your browser http://localhost:8099/pdf/123
7.A PDF view should open containing the sample PDF

8.Adjust the Dispatcher_Request method to parse the incoming request and load data from the database

Regards


(Jan Strid) #3

Thanks, Anton.

The server is an MS SQL server 2008 or possibly 2012, I can’t check that easily this time of day. The user interface is an aspx web site containing some dialogue and fueled by code-behind written in oxygen. I don’t know what more I could tell you, to be honest. I was looking for hints in the RemObjects reference material available on the web, but couldn’t even find a suitable heading.
I have written a fair bit of web site code for fun over the years, but I have never come across this particular challenge before, so I’m really in the dark. - Now, you referring to Remoting SDK actually helped me a bit on the way – now at least I know where to start looking. A quick look via google at https://docs.remotingsdk.com seems to indicate that what I need is a server component, built by yours truly and deployed, and some client code to communicate with that server code, am I right? Moreover, it sems I need to buy a Remoting SDK licence. If you can trace a misconception here, please stop me.
regards
J


(antonk) #4

Hello

I used only a tiny part of SDK in my code: I used it only to create something that would respond to requests from the browser. You already have a thing that does this - an IIS server that hosts your web-site. So you actually don’t need Remoting SDK. Ofc one day you could decide to switch from old-fashioned ASP.NET WebForms site to a SPA application built using AngularJS (only all tech names here are only for example) - in this case SDK can be used to create the REST services that will power this site. But for now you don’t need it.

This is not an Oxygene-specific thing. You would have to look for generic ASP.NET-related questions for this.
Remember that while you use Oxygene as a programming language, it still runs on the .NET platform. So in case of questions you just need to search not for ‘how to do this thing in Oxygene’ but for ‘how to do this thing in .NET’

I’ll try to compose a sample code for you


(antonk) #5

Here’s the sample. It is a web site created using the default Oxygene WebSite template.
The only differences are the link to the PDF file in the Default.aspx file and thePdfHandler.ashx web handler that actually does the work.

You’ll need to replace this line of code

var contents: array of Byte := System.IO.File.ReadAllBytes(HostingEnvironment.MapPath("~/App_Data/" + filename));

with actual code that reads the PDF file conents from the database.

EDIT: Added the missed archive: WebSite1.zip (603.9 KB)

Hope that helps


(Jan Strid) #6

Hi again, thanks for yr patience.
It’s getting late over here in Sweden, so maybe I am just confused for that reason, but your wording seems to indicate the presence of an attachment? (“the sample”?) There is nothing attached in the mail as received. Did I miss something?
J


(antonk) #7

Yes, sorry. It was quite late here too, so I forgot to attach it. Here it is: WebSite1.zip (603.9 KB)


(Jan Strid) #8

Thanks, I’ll have a look at it. It’s getting late again, so it’ll be tomorrow. Thanks for your understanding. I have just left business, and as a fresh-man senior citizen I do this much instead of doing crosswords and birdfeeding, as it were, so my absorption rate in terms of software product developments are sort-of limited. I have been googling a little in parallell to this conversation, however, and I think the fog is lifting. You have been helpful, thanks.
J