1. #1
    Sencha User
    Join Date
    Aug 2007
    Location
    Munich, Germany
    Posts
    135
    Vote Rating
    0
    gelleneu is on a distinguished road

      0  

    Default Error when downloading xls Excel File

    Error when downloading xls Excel File


    Hello,

    I'm a little confused. In some other threads, I have read, that creating an Excel File "on the fly", and showing a download-dialog for that is not so easy, even if you want to do this with an Ajax-Request and POST-Variables.

    So I go another way. My trick is to send an Ext.Ajax.Request with POST Data to a php file. This PHP-File writes an (physical) *.xls File with a specified Filename directly on the server.

    In my Callback method of the Ajax-Request, I will start downloading these xls-File with
    "location.href = http://mydomain.com/myexcel.xls".

    But when I call the location.href, I get an Error Message in Firebug:

    [Exception... "'<error>' when calling method: [nsIContentHandler::handleContent]" nsresult: "0x805d0001 (<unknown>)" location: "<unknown>" data: no]

    But the download Dialog appears, and download works correctly. What could be the Problem?

  2. #2
    Sencha User
    Join Date
    Aug 2007
    Location
    Munich, Germany
    Posts
    135
    Vote Rating
    0
    gelleneu is on a distinguished road

      0  

    Default


    I get this message too, if I try to download any xls File from the server. Possibly, the error occurs, because I've installed only OpenOffice Calc instead of MS Excel?

  3. #3
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,506
    Vote Rating
    54
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    Let me get this clear, you're sending two requests, one POST containing the whole of the Grid data, and in the callback, you're setting the location.href to something?

  4. #4
    Sencha User
    Join Date
    Aug 2007
    Location
    Munich, Germany
    Posts
    135
    Vote Rating
    0
    gelleneu is on a distinguished road

      0  

    Default


    Yes. I send Post data with an Ext.Ajax.Request to a php file.
    The php file creates an excel-file on the server, including the post data of the grid.
    Then the php files responses success = true and filename = export.xls.

    In the callback function I start location.href='http://myserver.com/'+response.responseText.filename

    What should I say? it works. But the extension .xls causes the error. When I save the file as f.e. export.zip, the download works without any errors, but I have to rename the exported file manually.

    I think, the problem is, that I haven't installed MS Excel on my PC. Instead of this I use OpenOffice Calc.

    But the solution itself works perfectly. The created file on the server will be overwritten on any export, so that everytime only one excel file on the server exists.

  5. #5
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,506
    Vote Rating
    54
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    The best thing to do would be to post the data NOT using Ajax, but using a hidden form element containing the data. Target the form at a hidden iframe.

    Then in your server code set the headers

    Code:
    Content-Type: application/vnd.ms-excel
    Content-Disposition: attachment;filename=whatever-you-want.xls
    And send back the Excel data.

    The browser sees that, and pops up the open/save prompt using the name you specify in the filename portion of the disposition.

    Just one request needed.

  6. #6
    Sencha User keypoint's Avatar
    Join Date
    Oct 2007
    Location
    Bucharest, Romania
    Posts
    69
    Vote Rating
    1
    keypoint is on a distinguished road

      0  

    Lightbulb Simplest way to generate post attachments

    Simplest way to generate post attachments


    The previous methods for generating attachments worked fine, if the method was GET.
    For POST, in order to obtain a download attachment window, you can use something like this; I am using the hdExport field to tell the form processing script to expot instead of process the form normally. What the code does is submit the form normally (not via Ajax), in a new window. Place this in the handler of your export button and you're done.

    PHP Code:
                        myForm().getForm().getEl().dom.target '_blank';
                        
    myForm().getForm().getEl().dom.action 'myUrl';
                        if (!
    Ext.fly('hdExport')) {
                            
    Ext.DomHelper.insertFirst(myForm().getForm().getEl(), {
                                
    tag'input',
                                
    type'hidden',
                                
    id'hdExport',
                                
    name'hdExport',
                                
    value'1'
                            
    });
                        }
                        
    myForm().getForm().getEl().dom.submit(); 

  7. #7
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,506
    Vote Rating
    54
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    Or, alternatively:

    Code:
    Ext.Ajax.request({
        url: 'myUrl',
        form: myForm,
        params: {hdExport: 1},
        isUpload: true
    });

  8. #8
    Sencha User keypoint's Avatar
    Join Date
    Oct 2007
    Location
    Bucharest, Romania
    Posts
    69
    Vote Rating
    1
    keypoint is on a distinguished road

      0  

    Default


    Heeey
    This is exactly what I needed, but didn't find a clear reference to it. Thanks!
    Much cleaner than my solution.
    Last edited by keypoint; 22 Apr 2008 at 1:42 PM. Reason: .

  9. #9
    Ext User
    Join Date
    Jul 2007
    Posts
    3,128
    Vote Rating
    1
    devnull is an unknown quantity at this point

      0  

    Default


    using "isUpload: true" has not worked reliably for me for this purpose; IE would appear to do nothing (FF worked fine). I have continued to use the hidden iframe method.

  10. #10
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,506
    Vote Rating
    54
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    isUpload forces Ext.Ajax to use a hidden iframe.

    If it doesn't work somewhere, there's a bug somewhere.