tors / jquery-fileupload-rails

jQuery File Upload integrated for Rails
669 stars 253 forks source link

[RFC] Remove Middleware #69

Open felixbuenemann opened 9 years ago

felixbuenemann commented 9 years ago

I propose to remove the Rack Middleware that alters some headers for the iframe transport.

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.

felixbuenemann commented 9 years ago

@tors @opti What do you think?

opti commented 9 years ago

@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.

tors commented 9 years ago

@felixbuenemann Reasonable points. Let's go ahead and remove the middleware.

jhubert commented 9 years ago

@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?

felixbuenemann commented 9 years ago

@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