locked
Open a new browser window in Lightswitch RRS feed

  • Question

  • I need the code to open a browser window via Lightswicth? I'm trying to open a pdf in a browser window.  Can I do it from a desktop application or must it be a web app?
    • Edited by Yann DuranModerator Wednesday, April 24, 2013 8:11 AM Fixed spelling mistake in title
    Thursday, April 18, 2013 3:38 PM

Answers

  • This certainly isn't my forte either but we should be able to get you through it...

    If you have the PDF data from the db, then you should be able to just have that stored in a buffer, yes?  In which case you wouldn't need the filestream and can just respond with the buffer, e.g. put this in page load of an aspx page.

            Dim strPDFName As String
            'assume the page request will have a parameter to specify which PDF to display
            strPDFName = Context.Request.QueryString.Item("pdfparm")
    
            'go get the bytes from the database and stuff them into the buffer
            Dim buffer() As Byte = New Byte(3) {48, 49, 50, 51}
    
            'write the response
            Context.Response.ContentType = "application/pdf"
            'Context.Response.Write("Hello World! <br />")
            Context.Response.OutputStream.Write(buffer, 0, buffer.Length)
            Context.Response.Flush()
            Context.Response.Close()
    
    

    Next, on an HTML page, you can put a command bar button.  Set the javascript in the tap method to open a new window calling the aspx page.  You'd need to submit a query string to the ASPX page to specify which PDF you want to open.  This snippet assumes that you have some way to send that from the client based on the record you're viewing at the time.

     

        window.open("../pdfDisplay.aspx?pdfparm=" + screen.someEntity.pdfName.toString());
    

    You'll have to modify that to fit your scenario but that should give you an idea.

    For #2, I'm not sure why System.Web is hiding from you, check your project references (depending on your VS settings, it's under the Project menu, then "Add reference..." or right-click on the server project in file view) - see if system.web can be checked there.

    I think for #3, depending on your UX you can just create the hyperlink in the client and have the target=_blank or use the approach above with the HTML client.

    Let me know if that helps...

    • Proposed as answer by Angie Xu Tuesday, May 7, 2013 6:16 AM
    • Marked as answer by Angie Xu Wednesday, May 8, 2013 12:56 AM
    Wednesday, April 24, 2013 9:35 PM
    Moderator

All replies

  • If you want to just open a web link, here is the simplest (and the best and the cheapest (free)).  I use it to open other web based Lightswitch apps.  Also to view SSRS reports.

    Download Here:

    Pixata custom controls for Lightswitch

    But, if you just want to view the PDF's in your app, here are a couple of options:

    LightSwitch Pdf Document Viewer Extension

    Component one has one too:

    Studio for LightSwitch ( I have bought this, but have not used it enough to credibly recommend it )


    Would someone please turn on the LIGHT?


    • Edited by JoeB_LS103 Thursday, April 18, 2013 4:16 PM
    Thursday, April 18, 2013 4:15 PM
  • From a browse or edit screen -- Add a data item (local property/string is just fine).

    Drag that new property into your display tree where ever you want it.

    Set the type to custom control.

    from the properties window, "edit render code"

    Make your code section look like this:

    myapp.BrowsePeople2Classes.linkme_render = function (element, contentItem) {
        // Write code here.
        var st = $('<a href="http://www.bing.com" target="_blank">Open BING site.</a>');
        st.appendTo($(element));

    };

    The Target= will open in a new window, remove, target="_blank", to make the same browser window open your linked site.

    You can also play with, img="mypic", but you will need your images located on a webserver somewhere and that is beyond the scope of this answer.

    Hope this Helps.

    Saturday, April 20, 2013 1:20 AM
  • Thanks for the replies.  I think I need to go a little deeper in my request.  I need to extract the PDF as a data binary file from the database (represented in an editable datagrid).  Show the data file in a browser window in PDF format.  My tactic has been to recreate the file as an .aspx page for it to render and as a page in a new window.  Help???
    Monday, April 22, 2013 9:30 PM
  • If I understand you correctly, you don't have a PDF file stored somewhere - just a binary stream in the database?  You could write a handler on the server to write the response, some examples:

    how to open new window and response.write to it

    Write PDF stream to response stream

    And then if you need to call it from the client, use an AJAX call to invoke the code on the server...

    Does that help?


    Monday, April 22, 2013 10:46 PM
    Moderator
  • Somewhat.  It's true that I need to take the binary stream to create the PDF.  I'm fairly knew to this but was able to retrieve the data out of the database.  I have several questions about how to get it thru a filestream to write to a web page.

    1.  When using Filestream it appears that I have to set a path string for the file to where I want it created?  Is that my only option?  If I create in a temp file, wouldn't I have to run it as a desktop for permission issues?

    2. Alot of the examples you gave were using the Response object to write the PDF content to be view via browser/web.  I was unable to import System.Web to access it.  How would I go about it?

    3. Once accessing it via Reponse, will I still need a handler to cause the content to show in a browser?

    Any help is appreciated, especially with code samples.  Thanks.

    Tuesday, April 23, 2013 9:39 PM
  • This certainly isn't my forte either but we should be able to get you through it...

    If you have the PDF data from the db, then you should be able to just have that stored in a buffer, yes?  In which case you wouldn't need the filestream and can just respond with the buffer, e.g. put this in page load of an aspx page.

            Dim strPDFName As String
            'assume the page request will have a parameter to specify which PDF to display
            strPDFName = Context.Request.QueryString.Item("pdfparm")
    
            'go get the bytes from the database and stuff them into the buffer
            Dim buffer() As Byte = New Byte(3) {48, 49, 50, 51}
    
            'write the response
            Context.Response.ContentType = "application/pdf"
            'Context.Response.Write("Hello World! <br />")
            Context.Response.OutputStream.Write(buffer, 0, buffer.Length)
            Context.Response.Flush()
            Context.Response.Close()
    
    

    Next, on an HTML page, you can put a command bar button.  Set the javascript in the tap method to open a new window calling the aspx page.  You'd need to submit a query string to the ASPX page to specify which PDF you want to open.  This snippet assumes that you have some way to send that from the client based on the record you're viewing at the time.

     

        window.open("../pdfDisplay.aspx?pdfparm=" + screen.someEntity.pdfName.toString());
    

    You'll have to modify that to fit your scenario but that should give you an idea.

    For #2, I'm not sure why System.Web is hiding from you, check your project references (depending on your VS settings, it's under the Project menu, then "Add reference..." or right-click on the server project in file view) - see if system.web can be checked there.

    I think for #3, depending on your UX you can just create the hyperlink in the client and have the target=_blank or use the approach above with the HTML client.

    Let me know if that helps...

    • Proposed as answer by Angie Xu Tuesday, May 7, 2013 6:16 AM
    • Marked as answer by Angie Xu Wednesday, May 8, 2013 12:56 AM
    Wednesday, April 24, 2013 9:35 PM
    Moderator