Click here to Skip to main content
15,115,261 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: Unselect text in textbox on textbox lostfocus event. Pin
sivaddrahcir20-Apr-09 8:26
Membersivaddrahcir20-Apr-09 8:26 
GeneralRe: Unselect text in textbox on textbox lostfocus event. Pin
pc.rajesh.singh24-Apr-09 0:53
Memberpc.rajesh.singh24-Apr-09 0:53 
GeneralRe: Unselect text in textbox on textbox lostfocus event. Pin
sivaddrahcir24-Apr-09 8:58
Membersivaddrahcir24-Apr-09 8:58 
QuestionHow to download file from a physical location in Silverlight? urgent......... Pin
salon17-Apr-09 4:49
Membersalon17-Apr-09 4:49 
AnswerRe: How to download file from a physical location in Silverlight? urgent......... Pin
Mark Salsbery17-Apr-09 9:42
MemberMark Salsbery17-Apr-09 9:42 
AnswerRe: How to download file from a physical location in Silverlight? urgent......... Pin
Braulio Dez17-Apr-09 10:01
MemberBraulio Dez17-Apr-09 10:01 
GeneralRe: How to download file from a physical location in Silverlight? urgent......... Pin
salon18-Apr-09 1:26
Membersalon18-Apr-09 1:26 
GeneralRe: How to download file from a physical location in Silverlight? urgent......... Pin
Braulio Dez18-Apr-09 10:06
MemberBraulio Dez18-Apr-09 10:06 
I don't have alink with the whole solution, but the pieces and what I implemented on my project.

Let's go step by step:

About the solution:

It's a workaround, SL cannot do that, what we do then is to workaround it using ASP .net / Javascript (in ASP .net it's possible to return a file to client as an attachment).

In my case:

--> I have an XML file on my client side (that represents the database diagram), it could be whatever that can be serializable.

--> I have an WCF service that accepts as input parameter that XML file, I send a call from SL to the service (if you have question on this give me a shout and will try to find some tutorial about how to call services from SL).

--> This service that I'm calling inherits the context from the ASP .net application, that is, we have the ASP .net session available, to enable this you only have to add to the header of your service class:
<br />
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]<br />
    public class RenderService<br />



--> In my case I stored the XML file in a session variable in the server (so for that user we get the file in his session / memory).

--> Once the Silverlight receives the callback from the service call (the service has been called successfully), I call from SL a javascript method:

public void ShowDiagramImageRenderPopup()
{
    System.Windows.Browser.HtmlPage.Window.CreateInstance("showReportDiagramPopup");
}


--> This javascript methods opens a popup that points to a URL (if I want it to download a file I just have to open a page in the same navigator, the only thing is that the custom HTTP Handler must return an "attacment").

// Show the report render popup, I'm calling an ASPX that has inside a refernce to an image generated by
// a custom HTTP hanlder (the commented solution, was the one related to download an attachmen)
function showReportDiagramPopup() {
    //http: //www.javascripter.net/faq/openinga.htm
    window.open("DiagramRenderPopup.aspx", "DatabaseDiagram", "scrollbars=yes, width=800,height=600", false);

    // NOT IN USE, rather using popup
    // Finally we are not going to use a popup, we just open the generated image as an attachment
    //window.location = "DiagramImageHandler.ashx"

}


--> Ok, now let's go for the CUstom HTTP Hanlder "DiagramImageHandler.ashx", the main code snippet that does the trick
public void ProcessRequest(HttpContext context)
{
    // Restore the XML from Session
    EntityExportDiagram exportDiagram = (EntityExportDiagram)HttpContext.Current.Session["printreport"];

    // In this case we generate a JPEG, we could generate another format (pdf, ...)
    Bitmap bitmap = new Bitmap(width, height,
        PixelFormat.Format32bppArgb);
    Graphics g = Graphics.FromImage(bitmap);

    // <draw_whatever_needed>
    //
    // Erase the background.
    //
    SolidBrush br = new SolidBrush(Color.White);
    g.FillRectangle(br, 0, 0, width, height);
    br.Dispose();

    (...)

    // </draw_whatever_needed>
    g.Dispose();

    // Output JPEG !! :-)
    context.Response.ContentType = "image/jpeg";//"image/gif";

    // ********** ATTACHMENT, What You need
    // Quite important !!!
    // We just tell that the image is an attachment, so we don't have to open a popup
    // just show the user the "open or save" box so he can browse the image or
    // save it to disk. If we remove this, the window.location will fail
    //context.Response.AddHeader("content-disposition", "attachment;filename=DBDiagram.jpg");
    // ********** /ATTACHMENT

    // Intermediate step needed to make it work:
    // Get content lenght tag
    // http://stackoverflow.com/questions/389503/why-asynchronous-ashx-generated-images-doesnt-always-work-in-ie6

    System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
    bitmap.Save(memoryStream, ImageFormat.Jpeg);
    context.Response.AddHeader("Content-Length", memoryStream.Length.ToString());
    memoryStream.WriteTo(context.Response.OutputStream);

    //bitmap.Save(context.Response.OutputStream, ImageFormat.Jpeg);
    context.Response.End();

    //
    // Clean up and return.
    //
    bitmap.Dispose();


    context.Response.Flush();
    context.Response.Close();

}


--> Then once the page is called, in my case a popup is shown with the diagram exported as JPEG, in your case (setting the attachment file) you will get a download / save file (watch out here, first time users will get a nasty IE warning and the SL application will reset, security stuff Frown | :-( ).

HTH
Braulio

PS.: Hope to get sometime some day and write a complete sample about this.

/// -------------------------
     Braulio Díez

     DBSchemaEditor.com
     Free Silverlight based DB Schema Modeling Tool
/// -------------------------

Questionapp.config --&gt; xaml? [modified] Pin
devvvy17-Apr-09 0:55
Memberdevvvy17-Apr-09 0:55 
AnswerRe: app.config --&gt; xaml? Pin
Pete O'Hanlon17-Apr-09 3:13
mvePete O'Hanlon17-Apr-09 3:13 
GeneralRe: app.config --&gt; xaml? Pin
devvvy17-Apr-09 6:58
Memberdevvvy17-Apr-09 6:58 
GeneralRe: app.config --&gt; xaml? Pin
Pete O'Hanlon17-Apr-09 10:17
mvePete O'Hanlon17-Apr-09 10:17 
GeneralRe: app.config --&gt; xaml? Pin
devvvy17-Apr-09 15:36
Memberdevvvy17-Apr-09 15:36 
GeneralRe: app.config --&gt; xaml? Pin
Pete O'Hanlon19-Apr-09 9:24
mvePete O'Hanlon19-Apr-09 9:24 
GeneralRe: app.config --&gt; xaml? Pin
Jammer18-Apr-09 0:23
MemberJammer18-Apr-09 0:23 
Questionsame Xaml to port a WPF app to Silverlight? Pin
devvvy17-Apr-09 0:50
Memberdevvvy17-Apr-09 0:50 
AnswerRe: same Xaml to port a WPF app to Silverlight? Pin
Mark Salsbery17-Apr-09 9:47
MemberMark Salsbery17-Apr-09 9:47 
Questionhosting problem from local host? Pin
Piyush Vardhan Singh17-Apr-09 0:47
MemberPiyush Vardhan Singh17-Apr-09 0:47 
AnswerRe: hosting problem from local host? Pin
Mark Salsbery17-Apr-09 9:50
MemberMark Salsbery17-Apr-09 9:50 
GeneralRe: hosting problem from local host? Pin
Piyush Vardhan Singh19-Apr-09 22:09
MemberPiyush Vardhan Singh19-Apr-09 22:09 
GeneralRe: hosting problem from local host? Pin
Mark Salsbery20-Apr-09 6:46
MemberMark Salsbery20-Apr-09 6:46 
QuestionTreeview in expander Pin
Ravi Mori16-Apr-09 21:01
MemberRavi Mori16-Apr-09 21:01 
AnswerRe: Treeview in expander Pin
pc.rajesh.singh17-Apr-09 6:03
Memberpc.rajesh.singh17-Apr-09 6:03 
QuestionResource dls Pin
Christian Graus16-Apr-09 16:56
protectorChristian Graus16-Apr-09 16:56 
AnswerRe: Resource dls Pin
Eslam Afifi16-Apr-09 18:06
MemberEslam Afifi16-Apr-09 18:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.