Skip to content

Instantly share code, notes, and snippets.

@jason-shen
Forked from EQuimper/uploadImage.ts
Created March 16, 2018 13:53
Show Gist options
  • Save jason-shen/ff116838bd8b0e60f502d8224ae95563 to your computer and use it in GitHub Desktop.
Save jason-shen/ff116838bd8b0e60f502d8224ae95563 to your computer and use it in GitHub Desktop.
Upload a image using presign url from s3 in React-Native
export async function uploadImage(method: string, url: string, file: any) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest()
xhr.open(method, url)
xhr.setRequestHeader('Content-Type', file.type)
xhr.onload = () => {
if (xhr.status !== 200) {
reject(
new Error(
`Request failed. Status: ${xhr.status}. Content: ${xhr.responseText}`
)
)
}
resolve(xhr.responseText)
}
xhr.send(file)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment