Skip to content

Instantly share code, notes, and snippets.

@4e4c52
Created August 25, 2011 13:06
Show Gist options
  • Save 4e4c52/1170604 to your computer and use it in GitHub Desktop.
Save 4e4c52/1170604 to your computer and use it in GitHub Desktop.
Upload files via AJAX using FormData
UploadFile = (f) ->
xhr = new XMLHttpRequest()
if (xhr.upload)
xhr.open("POST", "/admin/assets.json", true)
xhr.setRequestHeader("X_FILENAME", f.name)
# new_asset is the ID of my <form>
data = new FormData(document.getElementById('new_asset'))
data.append("asset[picture]", f)
xhr.send(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment