summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/libs/content-disposition.js
AgeCommit message (Collapse)Author
2018-07-13[IMP] web: replace iframe-based get_file by XHRXavier Morel
The error handling in get_file has always been somewhat problematic (sarcasm ho!). Pending PR #24854 attempts to add special handling for gateway timeout aside from its base stuff, and that's really quite terrible due to get_file's limitation of only being able to read the content of the document loaded in the iframe (no proper access to status codes & al). That was the occasion to look up modern API and see if we could do file downloads in JS without iframe crap. Turns out we can: xhr2 is well supported so we can POST, get a blob back, and create a "local download" for that in-memory blob using either a[download] (most browsers) or msSaveBlob (IE10+, that's actually a convenient-to-great API and it's a shame there's nothing like it in the standard). The changes are not trivial but save ~30LOC and most of the overhead is replicating the old/weird error handling as that requires additionally decoding the blob to a string & parsing that string to a document before being able to get the content (whereas before we'd just access to the document loaded in the iframe). OTOH we can now properly forward non-200 status codes to the crash manager (as name) which will allow using regular CM hooks to provide custom warning and error messages. Note that this requires two libraries: * a stripped down version of the content-disposition NPM lib, as download file names are provided via that header and parsing the header is decidedly non-trivial * the download.js library as it handles the various edge cases from different browsers, I'm don't think all of it is necessary for our use case but the library is not that big so no real point in trying to strip it out and potentially break it in the process