0

I want to Export a table from my database to an excel file on the server and then download it. The following is the code that I developed for this purpose:

 import flash.net.FileReference;
 import flash.net.URLRequest;
 public function sqlDownloadExcel():void {
      var http:HTTPService = new HTTPService;
      var parm:Object = new Object;
      var sql:String;

      sql = "insert into OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=C:\\inetpub\wwwroot\\myApp\\App_Data\\myExport.xls;', 'SELECT * FROM [Sheet1$]')  SELECT * FROM myTable";
      parm.sql = sql;
      parm.db = "myDatabase"; 
      http.url = "http://mywebsite.com/SQLconnector/sqlconnector.asp?irand="+Math.random();
      http.showBusyCursor = true;
      http.request = sql;
      http.addEventListener(ResultEvent.RESULT, function(e:ResultEvent):void {sqlDownloadExcelResult(e);});
      http.addEventListener(FaultEvent.FAULT, mssqlFault);
      http.method = "POST"; 
      sqlToken = http.send(parm);
 }
 public function sqlDownloadExcelResult(event:ResultEvent):void{
      var request:URLRequest = new URLRequest("http://mywebsite/myApp/App_Data/myExport.xls");
      var fileRef:FileReference = new FileReference();
      fileRef.download(request);                
 }

The code creates the Excel file on the server correctly, but running the fileRef.download(request) functions causes the following error:

Error #2176: Certain actions, such as those that display a pop-up window, may only be invoked upon user interaction, for example by a mouse click or button press.

Any advice would be much appreciated. Thank you

Fred
  • 378
  • 1
  • 10
  • 26

1 Answers1

1

You can't do what you want. Security restrictions prevent your app from trying to download anything to the clieht machine without user input. That is why you're seeing the error.

I recommend creating the excel sheet on the server, saving it to the server, and then sending a URL back to your Flex app. Use navigateToURL() to open it; and the browser should handle it based on the client's browsers settings. It may download automatically; or open automatically; or it may prompt the user what it wants to do.

If necessary, you can create a server side script to routinely clean out the generated files.

Based on the code you provided, it looks like you are 90% of the way there. Just instead of using FileReference to try to download the file, use navigateToURL() to open that URLRequest.

Alternatively, you could modify your UI to say "You're file is ready, click here to get it" or something similar. Then you have user interaction and can open the download window after the user clicks the button.

JeffryHouser
  • 39,401
  • 4
  • 38
  • 59