How to upload Audio or generic filetype

#1

for some reason posting an audio file as multipart/form-data doesn’t work the payload shows up as [object object]. below is a snippet where i get a signed url from google storage then post the form data to it, everything works but the request body is just [object object] instead of form data.

const data = new FormData()

    const meta = {
      uri, // "file://dsfsdfd/sfsfsd.caf"
      name: 'something.caf',
      type: 'audio/caf',
    }
    console.log(meta)
    data.append('file', meta)
    console.log('data pre send', data)
    try {
      const response = await fetch(`${signed_url}`, {
        body: data,
        method: 'PUT',
        headers: {
          'Content-Type': 'multipart/form-data',
        },
      })
      console.log('upload', response) // success
    } catch (e) {
      console.log('no upload', response)
    }
  }

  postImageToServer = (ticketId, params) => {
    return axios
      .post(`/tickets/${ticketId}/imageUpload`, params)
      .then(response => response.data, () => (this.state = 'error'))
  }
}
#2

it works, there is a bug in your code somewhere, see this example https://github.com/expo/examples/tree/master/with-formdata-image-upload

1 Like
#3

I’m not sure what I’m doing wrong everything looks right excpet the request body being [object object]
49 PM

#4

perhaps turn off chrome debugging

1 Like
#5

I have tried, I use a CORs pluging when it is on, and get a 200 response (not a resource policy warning)

#6

I tried setting up a snack but it doesn’t seem to work
https://snack.expo.io/HykAAergf

#7

does https://expo.io/@community/image-upload-example work for you?

#8

Hey yes it works,
I guess it’s just images.
Tried modifying it for audio https://snack.expo.io/S1QyIZSxf

#9

the example you shared doesn’t do anything - what is sound.caf? there is no such file. further, the backend that you’re using (the one from the example repo) is specifically intended for images: https://github.com/expo/examples/blob/master/with-formdata-image-upload/backend/index.js#L25-L37

#10

sound.caf is an audio file created with expo. it can be seen in the left under the files of the snack… Let’s close off this issue, I’ll explore some alternative solutions…

#11

You were right the remote debugging tools with the CORs plugin were malforming the request. Thank you for the help.