title: How can I prompt a user to open or save a PDF file returned by a .aspx file?
body: <p>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. </p>
  
  <p>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?</p>
  
  <p>Here are both versions of my ActionScript code:</p>
  
  <pre><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");
  }
  </code></pre>
  
  <p>FYI, here is the block of code that currently outputs the PDF or Excel file:</p>
  
  <pre><code>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);
  </code></pre>