I’ve done it like this:
var input = document.querySelector('input[type="file"]')
var data = new FormData()
data.append('file', input.files[0])
data.append('user', 'hubot')
fetch('/avatars', {
method: 'POST',
body: data
})
More Related Contents:
- Fetch: POST JSON data
- Handle response – SyntaxError: Unexpected end of input when using mode: ‘no-cors’
- How can I download a file using window.fetch?
- What limitations apply to opaque responses?
- How can I fetch an array of URLs with Promise.all?
- Fetch API request timeout?
- Fetch: reject promise and catch the error if status is not OK?
- Internal API fetch with getServerSideProps? (Next.js)
- Getting “TypeError: Failed to fetch” when the request hasn’t actually failed
- What is difference between Axios and Fetch?
- JavaScript, make a POST with JSON data with fetch()
- try..catch not catching async/await errors
- Fetch API vs XMLHttpRequest
- React useEffect causing: Can’t perform a React state update on an unmounted component
- fetch gives an empty response body
- Chrome – Fetch API cannot load file. How to workaround?
- How to use FormData in react-native?
- Using JavaScript Axios/Fetch. Can you disable browser cache?
- redux fetch body is not use with no cors mode
- fetch() does not send headers?
- How to handle HTTP code 4xx responses in fetch api
- Basic authentication with fetch?
- Relative paths with fetch in Javascript
- How do I download a file from FastAPI backend using Fetch API in the frontend?
- React cannot read property map of undefined
- Why does Fetch API Send the first PUT request as OPTIONS
- UseEffect being called multiple times
- fetch API returning an empty string
- React-native upload image to amazons s3
- How do I download a file from FastAPI backend using JavaScript Fetch API in the frontend?