Click here to Skip to main content
15,885,156 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: Unselect text in textbox on textbox lostfocus event. Pin
sivaddrahcir20-Apr-09 7:26
sivaddrahcir20-Apr-09 7:26 
GeneralRe: Unselect text in textbox on textbox lostfocus event. Pin
pc.rajesh.singh23-Apr-09 23:53
pc.rajesh.singh23-Apr-09 23:53 
GeneralRe: Unselect text in textbox on textbox lostfocus event. Pin
sivaddrahcir24-Apr-09 7:58
sivaddrahcir24-Apr-09 7:58 
QuestionHow to download file from a physical location in Silverlight? urgent......... Pin
salon17-Apr-09 3:49
salon17-Apr-09 3:49 
AnswerRe: How to download file from a physical location in Silverlight? urgent......... Pin
Mark Salsbery17-Apr-09 8:42
Mark Salsbery17-Apr-09 8:42 
AnswerRe: How to download file from a physical location in Silverlight? urgent......... Pin
Braulio Dez17-Apr-09 9:01
Braulio Dez17-Apr-09 9:01 
GeneralRe: How to download file from a physical location in Silverlight? urgent......... Pin
salon18-Apr-09 0:26
salon18-Apr-09 0:26 
GeneralRe: How to download file from a physical location in Silverlight? urgent......... Pin
Braulio Dez18-Apr-09 9:06
Braulio Dez18-Apr-09 9: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
devvvy16-Apr-09 23:55
devvvy16-Apr-09 23:55 
AnswerRe: app.config --&gt; xaml? Pin
Pete O'Hanlon17-Apr-09 2:13
mvePete O'Hanlon17-Apr-09 2:13 
GeneralRe: app.config --&gt; xaml? Pin
devvvy17-Apr-09 5:58
devvvy17-Apr-09 5:58 
GeneralRe: app.config --&gt; xaml? Pin
Pete O'Hanlon17-Apr-09 9:17
mvePete O'Hanlon17-Apr-09 9:17 
GeneralRe: app.config --&gt; xaml? Pin
devvvy17-Apr-09 14:36
devvvy17-Apr-09 14:36 
GeneralRe: app.config --&gt; xaml? Pin
Pete O'Hanlon19-Apr-09 8:24
mvePete O'Hanlon19-Apr-09 8:24 
GeneralRe: app.config --&gt; xaml? Pin
Jammer17-Apr-09 23:23
Jammer17-Apr-09 23:23 
Questionsame Xaml to port a WPF app to Silverlight? Pin
devvvy16-Apr-09 23:50
devvvy16-Apr-09 23:50 
AnswerRe: same Xaml to port a WPF app to Silverlight? Pin
Mark Salsbery17-Apr-09 8:47
Mark Salsbery17-Apr-09 8:47 
Questionhosting problem from local host? Pin
Piyush Vardhan Singh16-Apr-09 23:47
Piyush Vardhan Singh16-Apr-09 23:47 
AnswerRe: hosting problem from local host? Pin
Mark Salsbery17-Apr-09 8:50
Mark Salsbery17-Apr-09 8:50 
GeneralRe: hosting problem from local host? Pin
Piyush Vardhan Singh19-Apr-09 21:09
Piyush Vardhan Singh19-Apr-09 21:09 
GeneralRe: hosting problem from local host? Pin
Mark Salsbery20-Apr-09 5:46
Mark Salsbery20-Apr-09 5:46 
QuestionTreeview in expander Pin
Ravi Mori16-Apr-09 20:01
Ravi Mori16-Apr-09 20:01 
AnswerRe: Treeview in expander Pin
pc.rajesh.singh17-Apr-09 5:03
pc.rajesh.singh17-Apr-09 5:03 
QuestionResource dls Pin
Christian Graus16-Apr-09 15:56
protectorChristian Graus16-Apr-09 15:56 
AnswerRe: Resource dls Pin
Eslam Afifi16-Apr-09 17:06
Eslam Afifi16-Apr-09 17: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.