views:

1318

answers:

2

I have a Flex application that calls a .aspx page on our webserver, which builds a PDF or Excel File. Right now, I am using navigateToURL() to call the file, and this works fine when the output file is built successfully. However, if there is an error, or timeout, there is no way of knowing this in the Flash movie.

I am trying to use URLLoader instead, so that I can listen for an HTTP Status Code, and know when the load is complete, however, the URLLoader simply returns the bitmap data, with nothing prompting the user to open or save the output file. Is there anyway to do this?

Here are both versions of my ActionScript code:

private function buildFile():void
{
    var variables:URLVariables = new URLVariables();

    variables.rptName = "report1";

    variables.rptFormat = "PDF";

    var request:URLRequest = new URLRequest();

    request.url = "/buildFile.aspx";

    request.method = URLRequestMethod.POST;

    request.data = variables;

    var loader:URLLoader = new URLLoader();

    loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, 
                                        httpStatusHandler);

    loader.load(request);
}


private function buildFile():void
{
    var variables:URLVariables = new URLVariables();

    variables.rptName = "report1";

    variables.rptFormat = "PDF";

    var request:URLRequest = new URLRequest();

    request.url = "/buildFile.aspx";

    request.method = URLRequestMethod.POST;

    request.data = variables;

    navigateToURL(request, "_self");
}

FYI, here is the block of code that currently outputs the PDF or Excel file:

System.Web.HttpContext httpC;
httpC = System.Web.HttpContext.Current;
httpC.Response.Clear();
httpC.Response.ClearHeaders();
httpC.Response.Expires = 0;
switch (ReportFormat)
{
    case "Excel": Response.ContentType = "application/vnd.ms-excel";  break;

    case "PDF": Response.ContentType = "application/pdf"; ; break;

}

Response.AddHeader("Content-disposition", "attachment;filename=" + fileName);    

BinaryWriter binaryWriter = new BinaryWriter(httpC.Response.OutputStream);

binaryWriter.Write(result);
+4  A: 

You server should send "application/octet-stream" header when user requests that PSD file.

With ASP.NET it could look like this:

Response.ClearContent();
Response.ClearHeaders();
// with this header user will be promted to open or download file
Response.ContentType = "application/octet-stream";
// with this header, you will suggest to save file with "test.pdf" name
Response.AddHeader("Content-Disposition", "attachment; filename=test.pdf");
Response.WriteFile(Server.MapPath("~/test.pdf"));
Response.Flush();
Response.Close();

Also take a look here: Downloading files in Flex using the FileReference class

Koistya Navin
I'll have to check with the .NET developer to see how he is setting the Response Headers. I was hoping someone could help me on the Flex end.
Eric Belair
@Eric, why don't just redirect user to appropriate URL with PDF file?
Koistya Navin
We want to have the dialog displayed that gives the user the option to "Save" or "Open". The existing code works fine when I use navigateToURL() with "_self" as the target. But, there's no way to track the progress and/or completion. That is why I'm trying to use URLLoader.load().
Eric Belair
What does 'Response.ContentType = "application/octet-stream";' do differently?
Eric Belair
I was informed by my .NET developer that this will have no impact - it will not provide any different response/output than his original code. Guess it's back to the drawing board....
Eric Belair
if PDF file will be send with "application/octet-stream" header, user will be promted to save or open this file, otherwise PSD file will be opend right inside user's browser.
Koistya Navin
Maybe here you can find an answer: http://www.thetechlabs.com/video/creating-a-downloader-for-youtube-with-flexair-2/
Koistya Navin
Thanks. With URLLoader in Flex, those header setting still do not display the dialog to the user - it simply returns the binary data to the movie. Also, the option at that link only works with AIR, not Flex.
Eric Belair
A: 

Also set the content-disposition to "attachment" in the aspx page.

Joel Coehoorn