Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

250
Visualizações
How to attach file in the body while using fetch(for calling api) in react native

Hi I am new to React Native, and I was trying to call my API hosted on Heroku my code is below:

    const path = RNFS.ExternalDirectoryPath + '/newFile.jpg';
    const handleUploadFile = async () => {
    const token = await AsyncStorage.getItem('authtoken')
    const file = await RNFS.readFile(path, "base64");

    let url = `${host}/api/docs/add?card=${value}&number=${myFileId}`;
    console.log(url);
    let imageData = {
        uri: path,
        type: 'image/jpg', //the mime type of the file
        name: 'newFile'
    }
    let formData = new FormData();
    formData.append('file', imageData);
    const response = await fetch(url, {
        method: 'POST',
        mode: 'cors',
        cache: 'no-cache',
        credentials: 'same-origin',
        headers: {
            'authtoken': token,
            'Content-Type': 'multipart/form-data'
        },
        body: formData
    });
    const output = await response.json();
    console.log(output);
}

And while programming the server-side I tested my code as enter image description here

But while calling API from react-native I was getting the below error: enter image description here

Please help me in uploading file.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

after you reciving the file as base64 you should convert it to a file and after that you can append it to your formData:

const base64File = await RNFS.readFile(path, "base64");
const blobResult = await fetch(base64File);
const file = new File([blobResult], "newFile",{ type: "image/jpg" });

 const formData = new FormData();
 formData.append('file', file);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda