Note To Self: Prevent Uploads Hanging In Safari
I always learn a lot reading other people’s code. (Admittedly the lesson is sometimes to run in the opposite direction as fast as possible.) This morning I was looking at Basecamp’s file upload interface which is lovely and clean.
In each file’s form’s onsubmit handler I noticed a call to closeKeepAlive(). Here’s what it looks like:
/* A pretty little hack to make uploads not hang in Safari. Just call this
* immediately before the upload is submitted. This does an Ajax call to
* the server, which returns an empty document with the "Connection: close"
* header, telling Safari to close the active connection. A hack, but
* effective. */
function closeKeepAlive() {
if (/AppleWebKit|MSIE/.test(navigator.userAgent)) {
new Ajax.Request("/ping/close", { asynchronous:false });
}
}
Nice.
Posted in Notes To Self, Rails

4 Comments
Jump to comment form