Skip to content

Instantly share code, notes, and snippets.

@billhorsman
Forked from anonymous/file_upload.js.coffee
Created December 20, 2012 13:41
Show Gist options
  • Save billhorsman/4345379 to your computer and use it in GitHub Desktop.
Save billhorsman/4345379 to your computer and use it in GitHub Desktop.
$form.fileupload
dataType: "json"
done: (e, data) ->
result = if data.result?
# XHR (as used by, say, Chrome) gives you a simple JSON object
data.result
else
# iframe (as used by IE) gives you text back that you'll want to parse manually)
# Urgh. UTF8 value (passed by Rails form) causes JSON to fall over. Crappy solution on next line.
data = JSON.stringify(data).replace(/"name":"utf8","value":"[^"]*"/, '"name":"utf8","value":"REMOVED"')
JSON.parse(data).result
console.log result
class MyController < ApplicationController
def my_action
# Do whatever you need to do
respond_to do |format|
format.js {
render json: to_json(@agent)
}
format.html {
# iframe won't use .js it will just appear as .html - fool the client into thinking they're getting proper json back.
render text: "<textarea data-type=\"application/json\">#{to_json(@agent)}</textarea>"
}
end
end
end
@billhorsman
Copy link
Author

Some tweaks I had to make to get jQuery-File-Upload working with IE (without breaking other browsers in the process). See https://github.com/blueimp/jQuery-File-Upload

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment