question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't upload file using cross-fetch

See original GitHub issue

I am trying to upload a csv file using cross fetch with react-redux and I am getting Uncaught (in promise) Error: unsupported BodyInit type error. Full Error Trace:

Uncaught (in promise) Error: unsupported BodyInit type
at Request.Body._initBody (browser.js:227)
at new Request (browser.js:340)
at browser.js:422
at new Promise (<anonymous>)
at self.fetch (browser.js:421)
at Dropzone.RenderDropZoneInput._this.onDrop (RenderDropZoneInput.js:17)
at Dropzone.onDrop (index.js:229)
at Object.executeOnChange (LinkedValueUtils.js:132)
at ReactDOMComponent._handleChange (ReactDOMInput.js:241)
at Object.ReactErrorUtils.invokeGuardedCallback (ReactErrorUtils.js:69)
at executeDispatch (EventPluginUtils.js:85)
at Object.executeDispatchesInOrder (EventPluginUtils.js:108)
at executeDispatchesAndRelease (EventPluginHub.js:43)
at executeDispatchesAndReleaseTopLevel (EventPluginHub.js:54)
at Array.forEach (<anonymous>)
at forEachAccumulated (forEachAccumulated.js:24)
at Object.processEventQueue (EventPluginHub.js:254)
at runEventQueueInBatch (ReactEventEmitterMixin.js:17)
at Object.handleTopLevel [as _handleTopLevel] (ReactEventEmitterMixin.js:27)
at handleTopLevelImpl (ReactEventListener.js:72)
at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:143)
at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:62)
at Object.batchedUpdates (ReactUpdates.js:97)
at dispatchEvent (ReactEventListener.js:147)

Source Code:

class RenderDropZoneInput extends Component {
    onDrop = (acceptedFiles, rejectedFiles) => {
    const { onChange } = this.props.input
    const files = this.props.input.value || Immutable.List()
    const newFiles = Immutable.List(acceptedFiles)
    const allFiles = files.concat(newFiles)
    onChange(allFiles)
    console.log(acceptedFiles);
     let uri = "http://localhost:8080/api/uplaod"
     fetch(uri, {
      method: 'POST',
      mode: 'cors',
      headers: { 'Content-Type': 'application/octet-stream' },
      body: acceptedFiles[0]
    })

  }

  render() {
    const files = this.props.input.value
    return (
      <div>
        <CustomDropzone onDrop={this.onDrop} multiple={true}>
          {files.size ?
            files.map((file) => <img key={file.name} src={file.preview} style={{ width: '200px', maxWidth: '400px', flexGrow: 1, opacity: 0.5 }} alt='' />)
            : <div><p>Click on me!</p></div>}
        </CustomDropzone>
      </div>
    )
  }
}

export default RenderDropZoneInput

You can reproduce or access full project here: https://codesandbox.io/s/z39y9mlwq4

Steps to reproduce: Try uploading a file, the error will be displayed.

I have also tried using FormData

let form = new FormData()
form.append('file', acceptedFiles[0]);
return fetch(uri, {
  method: 'POST',
  mode: 'cors',
  headers: { 'Content-Type': 'application/octet-stream' },
  body: form//acceptedFiles[0]
})

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
lquixadacommented, Mar 14, 2018

@kueda actually if a native fetch is present on the environment, cross-fetch uses it. That includes React Native.

@himanshugpt recently we fixed an issue regarding FormData on cross-fetch. Do you mind testing it again?

2reactions
himanshugptcommented, Feb 4, 2018

Closing this one. Moved to fetch: https://github.com/github/fetch/issues/601

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Fetch API with cross-fetch polyfill to upload file on file ...
Turns out your import fetch from "cross-fetch" is shimming fetch incorrectly, by breaking the parsing of blobs and FormData.
Read more >
cross-fetch - npm
Universal WHATWG Fetch API for Node, Browsers and React Native. ... Start using cross-fetch in your project by running `npm i cross-fetch`.
Read more >
Using fetch to do a file upload - YouTube
In this video, we are using fetch to do a file upload. File uploads could be hard if you haven't done it before,...
Read more >
Using Fetch API with cross-fetch polyfill to upload file on file ...
Coding example for the question Using Fetch API with cross-fetch polyfill to upload file on file-input change-Reactjs.
Read more >
POST method uploads - Manual - PHP
Be sure your file upload form has attribute enctype="multipart/form-data" otherwise the file upload will not work. The global $_FILES will contain all the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found