Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6290

upload mp3 to server doesn't work in react-native

$
0
0

I have this code to upload mp3 audio to a server but it gives me [TypeError: Network request failed]Help please

export async function uploadFile(fileUri) {  const url = //my url;  var token;  await AsyncStorage.getItem('access_token').then((res) => {    token = res;  });  let data = new FormData();  data.append('file', {    uri: fileUri,    name: 'test.mp3',    type: 'audio/mpeg',  });  return fetch(url, {    method: 'POST',    headers: {      Authorization: 'Bearer '+ token,'Content-Type': 'multipart/form-data',    },    body: data,  })    .then((response) => response.json())    .catch((error) => console.error(error));}

The same exact code works fine for an image uri if I change

data.append('file', {    uri: fileUri,    name: 'test.mp3',    type: 'audio/mpeg',  });

to

 data.append('file', {    uri: fileUri,    name: 'test.jpg',    type: 'image/jpeg',  });

I'm sure the audio's uri is correct because I use the exact same way to get the uri of the image which works fine.

I tried audio/mpeg and audio/mp3 for the type but neither of them works.


Viewing all articles
Browse latest Browse all 6290

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>