0

I am trying to send a file to a signed URL using PUT request but the file is not uploading. Below is my code:

const uploadToBucket = async (uploadUrl, fileUri, contentType, fileSize) => {
    try {
      const fileResponse = await fetch(fileUri);
      const blob = await fileResponse.blob(); 
      const response = await axios.put(uploadUrl, blob, {
        headers: {
          'Content-Type': contentType,
          'x-goog-content-length-range': `0,${fileSize}`,
        },
      });

      // 3. Check response status
      if (response.status == 200 ) {
        console.log('File uploaded successfully:', response.data);
      } else {
        console.error(`Upload failed: ${response.status} - ${response.data}`);
        Alert.alert('Upload Error', 'Failed to upload file. Please try again.');
      }
    } catch (error) {
      console.error('Error while uploading file:', error);
      Alert.alert('Error', 'Something went wrong during the file upload.');
    }
  };

I also tried sending file as a formData but it does not work either. rnfs.readFile() I also tried but It cannot not read big files.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.