Open from memory

Nov 28, 2011 at 3:11 PM

Hi, I have a project using Silverlight/RIA Services where users can save documents to a database  through a form,

Is it possible with interlink to allow them to open documents inline knowing that these documents are not on the filesystem but on the database and they're loaded as byte[] with RIA Services?

Thanks and congrats for these interesting project!

Nov 28, 2011 at 4:08 PM

Yes you can. HSS Interlink supports downloading in addition to uploading. You just need to implement a File Download Handler (not to be confused with an HTTP Handler).

In the file handler you would just call your your database to get the bytes. It supports returning the bytes directly to Silverlight, in addition to opening the files to the browser directly as INLINE or ATTACHMENT.

If you read this here the first half discusses building your download handler. The example uses the local file system, but you can just replace the File.IO calls with Database calls.

Let's us know if you have any more questions.

Nov 28, 2011 at 4:22 PM
Edited Nov 28, 2011 at 4:24 PM

Also to further clarify. Since you will be extracting the file bytes from the database, you can bypass the this.SendFile() method (if you don't want to safe the bytes to disk first) and just call the response object directly. There are benefits of saving to disk though and letting IIS Transmit the file. Meaning if you send the bytes directly yourself, you worker process will be the one busy sending the bytes. If you safe to disk first, and let IIS transmit the file, it offloads the work, freeing up your workprocess. If your files are small, then it's not a big issue, but if they're big you will begin to suffer from memory pressure.

Example of direct response: (the below would be for the OpenInline method)

this.Context.Response.ContentType = YOUR-CONTENT-TYPE;
this.Context.Response.AppendHeader("Content-Length", BYTES.Length.ToString());
this.Context.Response.AppendHeader("Content-Disposition", "inline; filename=" + YOUR-FILE-NAME);
this.Context.Response.ExpiresAbsolute = DateTime.UtcNow.Subtract(new TimeSpan(1, 0, 0, 0));

From the Silverlight client, you just call DownloadFileDialog.OpenFileInline(serverSideFileName);
This will open the file in the browser, not your silverlight application.
To open in your app:

string fileName = "serverSideFileName"; // The file to download from the server 
var ddialog = new DownloadFileDialog(saveFileDialog.SafeFileName, saveFileDialog.OpenFile(), fileName);
Nov 29, 2011 at 8:44 AM
Edited Nov 29, 2011 at 11:24 AM

Hi Hyspdrt,

First thanks for your quick answers!

I try to implement your first solution but I get the following error : "Missing the UploadHandler from the web.config"

I didn't understand because I follow the quickstart exemple and put the following lines in the applicationSettings of the web.config :

            <add key="DownloadHandler" value="Test.Web.DownloadHandler, Test.Web"/>
            <add key="UploadHandler" value="Test.Web.UploadHandler, Test.Web"/>
where value is the namespace+name of my handler and Test.Web is the name of my webproject
Edit : It works , I found my mistake: I put in <applicationSettings> instead of <appsettings>
Nov 29, 2011 at 4:46 PM

Glad to hear you got it working. Let us know if you need anything else.