jquery-fileupload-rails
jquery-fileupload-rails copied to clipboard
[RFC] Remove Middleware
I propose to remove the Rack Middleware that alters some headers for the iframe transport.
- In the past (and currently) the required patches to the jquery-file-upload javscript sources have often been neglected, so it was mostly not active
- It is not generally needed, if the proper content type is returned from the controller that handles uploads and the jquery file upload wiki already contains instruction on how to do that
- It modifies the body to add some wrapper html code to inject original response data, which is no longer present in recent versions of rails due to the streaming support wrapping the original rack response
- Above mentioned wrapper html breaks non-html content handling in the jquery iframe transport (json, xml, script), because the transport expects the iframe body to contain either json, xml or javascript, but not the html/textarea wrapper added by the middleware
It's also hard to adapt the middleware to handle all the different cases of data that might be returned and is supported by the jquery iframe transport, so this is better handled inside the app's controller.
@tors @opti What do you think?
@felixbuenemann I have no objections on that. However, I have never faced to the necessity of using this middleware for all the time I have been using this gem.
@felixbuenemann Reasonable points. Let's go ahead and remove the middleware.
@felixbuenemann I've been struggling with iframe transport and rails and this middleware helped. Are you able to give some examples as to how you handle this in the controller without the middleware?
@jhubert I haven't looked at this for a long time. It seems that at the very least the client would have to add a header like 'X-Requested-With: IFrame' so the controller could detect iframe requests and then alter the Content-Type to text/html so eg IE wouldn't show a download prompt. It can then return the response wrapped in a textarea which has data-type, data-status and data-statusText attributes with the relevant data. This is documented in the jquery-iframe-transport.js docs.
I do remember discussing this issue upstream, where I did a PR to include the 'X-Requested-With: IFrame' header in the ajax request, but it was rejected because supposedly there's another way to detect this. If your controller only accepts requests from jquery file uploader, then for non-iframe request request.xhr?
will be truthy and for iframe requests if will be falsy. This technique does not help you if you get normal form uploads without js to the same controller action, because they will look the same as the iframe upload.
You can then do something like this:
respond_to do |format|
format.html do
if request.xhr?
# render normal response
else
# render response wrapped in textarea for iframe transport
end
end
end