Download excel file xhr
Months have passed since I posted this, finally I've found a better approach to speed things up when working with big base64 strings. I now store base64 strings into the database using longtext or longblog fields , then I pass its record ID through the jQuery File Download, finally on the download script file I query the database using this ID to pull the base64 string and pass it through the download function.
I know this is way beyond what the OP asked, however I felt it would be good to update my answer with my findings. For those looking a more modern approach, you can use the fetch API. The following example shows how to download a spreadsheet file.
It is easily done with the following code. Also, it has a similar syntax to the jQuery approach, without the need to add any additional libraries. Of course, I would advise checking to which browser you are developing, since this new approach won't work on IE.
You can find the full browser compatibility list on the following [link][1]. This url must be set, on my example I am assuming you know this part. Also, consider the headers needed for your request to work.
I want to point out some difficulties that arise when using the technique in the accepted answer, i. You can't set headers on the request. If your authentication schema involves headers, a Json-Web-Token passed in the Authorization header, you'll have to find other way to send it, for example as a query parameter.
You can't really tell when the request has finished. Well, you can use a cookie that gets set on response, as done by jquery. It won't work for concurrent requests and it will break if a response never arrives. You can only use the content types supported by a form. Which means you can't use JSON. I ended up using the method of saving the file on S3 and sending a pre-signed URL to get the file. As others have stated, you can create and submit a form to download via a POST request. However, you don't have to do this manually.
One really simple library for doing exactly this is jquery. It provides an API similar to the standard jQuery. This is a 3 years old question but I had the same problem today. I looked your edited solution but I think that it can sacrifice the performance because it has to make a double request. So if anyone needs another solution that doesn't imply to call the service twice then this is the way I did it:.
This form is just used to call the service and avoid to use a window. After that you just simply have to make a form submit from jquery in order to call the service and get the file. It's pretty simple but this way you can make a download using a POST.
I now that this could be easier if the service you're calling is a GET , but that's not my case. I used this FileSaver. In my case with csv files, i did this in coffescript :. I think for most complicated case, the data must be processed properly. Under the hood FileSaver. To get Jonathan Amends answer to work in Edge I made the following changes:. Below is my solution for downloading multiple files depending on some list which consists of some ids and looking up in database, files will be determined and ready for download - if those exist.
And Yes, like others said, it is possible to do it in jQuery Ajax. I did it with Ajax success and I am always sending response As long as you return response , success in Ajax can work with it, you can check if file actually exist or not as the line below in this case would be false and you can inform user about that:. But I am referring to a page that must first be processed and then downloaded.
I know browsers block multiple file downloads, and I also have API which returns a set of csv formatted data. If anyone can help me improve this that would be great, but it's working for me so far.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Handle file download from ajax post Ask Question. Asked 8 years, 7 months ago. Active 1 year, 1 month ago. Viewed k times. Improve this question. Pavle Predic Pavle Predic 5, 3 3 gold badges 15 15 silver badges 16 16 bronze badges.
For those who read this article, read this post: stackoverflow. Add a comment. The various file formats use different internal names for file properties. The workbook Props object normalizes the names:. Names is an array of defined name objects which have the keys:.
Excel allows two sheet-scoped defined names to share the same name. However, a sheet-scoped name cannot collide with a workbook-scope name. Workbook writers may not enforce this constraint. Views is an array of workbook view objects which have the keys:. Even for basic features like date storage, the official Excel formats store the same content in different ways. The parsers are expected to convert from the underlying file format representation to the Common Spreadsheet Format.
Writers are expected to convert from CSF back to the underlying file format. The A1-style formula string is stored in the f field. Even though different file formats store the formulae in different ways, the formats are translated. Shared formulae are decompressed and each cell has the formula corresponding to its cell.
Writers generally do not attempt to generate shared formulae. Cells with formula entries but no value will be serialized in a way that Excel and other spreadsheet tools will recognize. This library will not automatically compute formula results! Array formulae are stored in the top-left cell of the array block. All cells of an array formula have a F field corresponding to the range. A single-cell formula can be distinguished from a plain formula by the presence of F field.
For a multi-cell array formula, every cell has the same array range but only the first cell specifies the formula. Utilities and writers are expected to check for the presence of a F field and ignore any possible formula element f in cells other than the starting cell.
They are not expected to perform validation of the formulae! Note that string literals are prefixed with an apostrophe ' , consistent with Excel's formula bar display. Since Excel prohibits named cells from colliding with names of A1 or RC style cell references, a not-so-simple regex conversion is possible. OpenFormula formulae can be converted with regular expressions. There are three different width types corresponding to the three different ways spreadsheets store column widths:.
SYLK and other plain text formats use raw character count. Contemporaneous tools like Visicalc and Multiplan were character based. Since the characters had the same width, it sufficed to store a count. This tradition was continued into the BIFF formats. Column widths, row heights, and other measures use pixels. When the pixel and character counts do not align, Excel rounds values.
The Max Digit Width is the width of the largest digit when rendered generally the "0" character is the widest. The internal width must be an integer multiple of the the width divided by ECMA describes a formula for converting between pixels and the internal width.
This represents a hybrid approach. Read functions attempt to populate all three properties. Write functions will try to cycle specified values to the desired type.
In order to avoid potential conflicts, manipulation should delete the other properties first. For example, when changing the pixel width, delete the wch and width properties. Given the constraints, it is possible to determine the MDW without actually inspecting the font! The parsers guess the pixel width by converting from width to pixels and back, repeating for all possible MDW and selecting the MDW that minimizes the error.
XLML actually stores the pixel width, so the guess works in the opposite direction. Even though all of the information is made available, writers are expected to follow the priority order:. Note: Excel UI displays the base outline level as 1 and the max level as 8.
The level field stores the base outline as 0 and the max level as 7. Excel internally stores row heights in points. For different resolutions they may not agree, so the library separates the concepts. The cell. If the format is not specified, the Excel General format is used. The format can either be specified as a string or as an index into the format table.
Parsers are expected to populate workbook. SSF with the number format table. Writers are expected to serialize the table. Custom tools should ensure that the local table has each used format string somewhere in the table.
Excel convention mandates that the custom formats start at index The following example creates a custom format from scratch:. The rules are slightly different from how Excel displays custom number formats. In particular, literal characters must be wrapped in double quotes or preceded by a backslash. It makes sense when the producer and consumer of files are in the same locale, but that is not always the case over the Internet.
To get around this ambiguity, parse functions accept the dateNF option to override the interpretation of that specific format string. Hyperlinks are stored in the l key of cell objects.
The Target field of the hyperlink object is the target of the link, including the URI fragment. Tooltips are stored in the Tooltip field and are displayed when you move your mouse over the text. Note that Excel does not automatically style hyperlinks -- they will generally be displayed as normal text.
Relative Paths have undefined behavior in the SpreadsheetML format. Excel will treat a.. Links where the target is a cell or range or defined name in the same workbook "Internal Links" are marked with a leading hash character:. Cell comments are objects stored in the c array of cell objects. The actual contents of the comment are split into blocks based on the comment author.
The a field of each comment object is the author of the comment and the t field is the plain text representation. Names longer than 54 characters may cause issues with other formats. Excel enables hiding sheets in the lower tab bar. The sheet data is stored in the file but the UI does not readily make it available.
Standard hidden sheets are revealed in the "Unhide" menu. Excel also has "very hidden" sheets which cannot be revealed in the menu. It is only accessible in the VB Editor!
The visibility setting is stored in the Hidden property of sheet props array. Non-Excel formats do not support the Very Hidden state. The best way to test if a sheet is visible is to check if the Hidden property is logical truth:.
VBA Macros are stored in a special data blob that is exposed in the vbaraw property of the workbook object when the bookVBA option is true. The supported format writers automatically insert the data blobs if it is present in the workbook and associate with the worksheet names. The workbook code name is stored in wb. Worksheet and Chartsheet code names are in the worksheet properties object at wb.
Macrosheets and Dialogsheets are ignored. The readers and writers preserve the code names, but they have to be manually set when adding a VBA blob to a different workbook. Older versions of Excel also supported a non-VBA "macrosheet" sheet type that stored automation commands. These are exposed in objects with the! The vbaraw field will only be set if macros are present, so testing is simple:.
The exported read and readFile functions accept an options argument:. Strings can be interpreted in multiple ways. The type parameter for read tells the library how to parse the data argument:. Excel and other spreadsheet tools read the first few bytes and apply other heuristics to determine a file type.
This enables file type punning: renaming files with the. This library applies similar logic:. DBF files are detected based on the first byte as well as the third and fourth bytes corresponding to month and day of the file date.
Excel is extremely aggressive in reading files. This library attempts to replicate that behavior. The best approach is to validate the desired worksheet and ensure it has the expected number of rows or columns. Extracting the range is extremely simple:. The exported write and writeFile functions accept an options argument:. For broad compatibility with third-party tools, this library supports many output formats.
The specific file type is controlled with bookType option:. The type argument for write mirrors the type argument for read :. Numbers, Booleans and Strings are stored as the corresponding styles. Dates are stored as date or numbers. Array holes and explicit undefined values are skipped. All other values are stored as strings.
The function takes an options argument:. The default column order is determined by the first appearance of the field using Object. The function accepts an options argument:. The original sheet cannot be reproduced using plain objects since JS object keys must be unique. Numbers are parsed. All other data will be stored as strings. String literals are prefixed with a ' in accordance with Excel. The txt output type uses the tab character as the field separator.
If the codepage library is available included in full distribution but not core , the output will be encoded in CP and the BOM will be prepended. Despite the library name xlsx , it supports numerous spreadsheet file formats:. Features not supported by a given file format will not be written. Formats with range limits will be silently truncated:.
Excel SpreadsheetML range limits are governed by the version of Excel and are not enforced by the writer. Excel does not follow the specification, and there are additional documents discussing how Excel deviates from the specification. The structure is largely similar to the Lotus file formats. You can add your comment about this article using the form below.
Make sure you provide a valid email address else you won't be notified when the author replies to your comment Please note that all comments are moderated and will be deleted if they are Not relavant to the article Spam Advertising campaigns or links to other sites Abusive content. Please do not post code, scripts or snippets. Required Invalid Email Address. Security code:. Required Invalid security code. I declare, I accept the site's Privacy Policy.
Add Comment. Disclaimer : The code samples and API available at www. You are free to use it for commercial as well as non-commercial use at your own risk, but you cannot use it for posting on blogs or other tutorial websites similar to www.
All the code samples and API provided by the authors are solely their creation and neither the author nor the site are responsible if it does not work as intended.