Search code examples
c#silverlightsilverlight-oob

Opening up PDFs and other documents from Silverlight out of browser


I'm having a little problem figuring out the best way to open up a file that I have stored in a database. The file is being stored as a byte array in a nvarbinary field in the database. Currently when I want to open up a file I use an ASP.NET webpage that I pass a variable to and write the file stream to the page. This works fine when using the in browser version of the Silverlight application, but when out of browser I cannot invoke a browser window to open because I don't have access to dom.

How can I open the bytearray from Silvelright without invoking a browser window? I'm able to pass the bytearray and file type to the Silverlight app no problem. I just don't know how to display it once I have it there..

Thanks!


Solution

  • If you are targeting windows (with full trust enabled, and not mac), you can do this out-of-browser by first writing the file to disk (either in isolated storage or in My Documents), then using a WScript.Shell COM object to have the OS open the file.

    After you have saved the byte stream to a file and have the file location, you can do:

    using (dynamic shell = AutomationFactory.CreateObject("WScript.Shell"))
    {
        shell.Run(fileLocation); //works similar to start -> run -> filename
    }
    

    If you want to leverage your existing ASP page, you can pass its URL to shell.Run and the OS will use the user's default browser to open that page.

    On a mac, the best you could do is save the file to their user directory, and have them manually navigate there with finder and double-click it.