React Native returns "Stream Closed" when uploading image using expo-image-picker

19 Views Asked by At

  const pickImage = async () => {
    setIsLoading(true);
    let result = await ImagePicker.launchImageLibraryAsync({
      mediaTypes: ImagePicker.MediaTypeOptions.All,
      allowsEditing: true,
      aspect: [4, 3],
      quality: 1,
    });
    setIsLoading(false);

    if (!result.canceled) {
      setAvatar(result.assets[0]);
    }
  };

const handleSubmit = (readerInfo) => {
        const { phone_num, birth_date, email_address, gender, first_name, last_name, address } = readerInfo;
        setIsLoading(true);
        const formData = new FormData();
    
        if (!user_id) {
          return;
        }
    
        if (avatar?.uri) {
          formData.append("avatar", {
            uri: avatar.uri,
            name: "reader-avatar",
            type: avatar.mimeType,
          });
        }
    
        user_id && formData.append("user_id", user_id);
        phone_num && formData.append("phone_num", phone_num.trim());
        birth_date && formData.append("birth_date", birth_date);
        formData.append("email_address", email_address.trim());
        address && formData.append("address", address.trim());
        formData.append("gender", gender.value);
        first_name && formData.append("first_name", first_name.trim());
        last_name && formData.append("last_name", last_name.trim());
    
        const configurations = {
          method: "PUT",
          url: `http://10.0.2.2:5000/users/reader`,
          data: formData,
          headers: {
            // Accept: "application/json",
            "Content-Type": "multipart/form-data",
          },
        };
    
        axios(configurations)
          .then((result) => {
          })
    }

After picking image, i upload it to nodejs server, but in the first time, i get error: "Stream Closed", i have to try for the second time then it will work I have try many ways to solve this problem but it doesn't work :(( How can i solve this problem? Any solution? Thanks

0

There are 0 best solutions below