6

Contrary to popular opinion, I still firmly stand by my belief that you should thoroughly study something in-depth before you attempt to do anything serious with it. Failure to do so will have an enormous cost of waste of time attached to it.

Here's an example:
I was using AJAX to post a multi-part request containing a file.
Now here was the problem: no matter what code I forced in the backend, the browser would in all cases refuse to prompt a SaveFileDialog (and I had turned on the option in the browser to ask the user if download). This took me two entire days and at least 100 Google queries and several RFCs to figure out.

From StackOverflow:
The cause was simply that you can't (typically) make a browser prompt a SaveFileDialog via an AJAX request, even if you set the necessary headers. Why? The browser will just dump everything back into the XmlHttpRequest object..

If you make a regular request with Content-Disposition: attachment; and so on, then it works, but yeah, not with an XmlHttpRequest.

Conclusion:
Had I better studied the HTTP spec, networking and AJAX in-depth, I would have instantly known what the cause was.

Comments
  • 0
    I have to disagree.
    I was working on a similar thing (Actually the same) about 5 years ago. It took me 5 minutes to find this problem and a solution (SO with some custom adjustments) (Simply create a “form” on the fly, append it to your document and POST it in a new tab, then remove from your document. Works like a charm).

    You don’t need to read whole HTTP spec
  • 0
    @NoToJavaScript That's not what the form was designed for and it's anti-standard, non-JS6 way of working. I discussed that with the web team and they disapproved that approach as being dirty.

    And anyone who's read any book on the web knows that the web is to be standardized, its entire premise is that (e.g. living standard, w3c, polyfill, HTTP spec, etc.). The very reason for a broken web is selfish authors writing their own hacky solutions. The web consortium works hard every year to have a standard web.
  • 0
    @PublicByte

    WTF, POST form and get a file in return exists since version 1.0 of WEB.
    It uses NOTHING what is not standard and even works in IE. (Not what I care, we dropped IE about 4 years ago).
    And BTW it is no longer an AJAX call, but a simple POST to an API endpoint.

    Complex ? Not really, it’s 4-5 lines of JavaScript.

    And 2 days on that problem? You work in very slow-paced environment. That task would be estimated at best at 3-4 hours where I am.

    Edit : And new tab auto closes, when browser detects that it's a file.
  • 0
    My thoughts reading this:

    Content-Disposition is a response header. The browser handlers expect to see it only on the response so they can bind it to the correct mechanism. Tldr, it's a server side behavior, the server echoing it back to you from the request is a coincidence.

    In order to get this functionality from the browser you'll need to artificially trigger the same handler, encode the response data into a blob, then into a fake memory mapped url using createObjectURL. You then simulate a click on a programatically generated anchor featuring the download attribute to trigger the correct handler pipeline.

    That or use the filesystem html5 API and write your own saving dialog.
  • 1
    Why is everyone always flaming inside my posts? Lmao.
  • 0
    @SortOfTested See, you have more knowledge than I do. Thank you for proving my point. :P
  • 0
    @NoToJavaScript I work in an environment where quality and research go above speed.
Add Comment