Invalid file extension error after uploading file using document picker in react native

115
August 14, 2019, at 9:30 PM

I'm using react-document picker for uploading excel file to php mysqli server using json.file path and details are saved to server database perfectly, but the file moved file is showing corrupted and invalid format, when format is correct, i think the problem is the content to move to file is not correct, i think im not sending correct content through json, how can i get content of file from document picker. Below Is the code:

  selectPhotoTapped() {
    const options = {
      quality: 1.0,
      maxWidth: 500,
      maxHeight: 500,
      storageOptions: {
        skipBackup: true
      }
    };
        DocumentPicker.show(
      {
        filetype: [DocumentPickerUtil.allFiles()],
        //All type of Files DocumentPickerUtil.allFiles()
        //Only PDF DocumentPickerUtil.pdf()
        //Audio DocumentPickerUtil.audio()
        //Plain Text DocumentPickerUtil.plainText()
      },
      (error, res) => {
        this.setState({ fileUri: res.uri });
        this.setState({ fileType: res.type });
        this.setState({ fileName: res.fileName });
        this.setState({ fileSize: res.fileSize });
                this.setState({

          data: JSON.stringify(res),

        });
        console.log('res : ' + JSON.stringify(res));
        console.log('URI : ' + res.uri);
        console.log('Type : ' + res.type);
        console.log('File Name : ' + res.fileName);
        console.log('File Size : ' + res.fileSize);
      }
    );


  }

  uploadImageToServer = () => {
    RNFetchBlob.fetch('POST', 'http://192.168.43.217/Pros/upload_image.php', {
      Authorization: "Bearer access-token",
      otherHeader: "foo",
      'Content-Type': 'multipart/form-data',
    }, [
        { name: 'excfile', filename: 'excfile.xlsx', type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', data: this.state.data },
        { name: 'image_tag', data: this.state.Image_TAG }
      ]).then((resp) => {
        var tempMSG = resp.data;
        tempMSG = tempMSG.replace(/^"|"$/g, '');
        Alert.alert(tempMSG);
      }).catch((err) => {
        // ...
      })
  }

PHP SIDE:

$domain_name = "http://192.168.43.217/Pros/" ;
    // Image uploading folder.
    $target_dir = "uploads";
    // Generating random image name each time so image name will not be same .
    $target_dir = $target_dir . "/" .rand() . "_" . time() . ".xlsx";
    // Receiving image tag sent from application.
    $img_tag = $_POST["image_tag"];
    // Receiving image sent from Application    
    if(move_uploaded_file($_FILES['excfile']['tmp_name'], $target_dir)){
        // Adding domain name with image random name.
        $target_dir = $domain_name . $target_dir ;

        // Inserting data into MySQL database.
        mysql_query("insert into image_upload_table ( image_tag, image_path) VALUES('$img_tag' , '$target_dir')");
        $MESSAGE = "File Uploaded Successfully." ;
        // Printing response message on screen after successfully inserting the image . 
        echo json_encode($MESSAGE);
    }
Rent Charter Buses Company
READ ALSO
How to run over an array of objects and connect a specific field in those objects [on hold]

How to run over an array of objects and connect a specific field in those objects [on hold]

I have an array of users and each user has an Age field, and I want to get a sum of all the user's age fields

121
ComponentDidMount Code Executing even after component unmounts

ComponentDidMount Code Executing even after component unmounts

Inside ComponentDidMount() we call an api periodically to get dataBut the api is still getting called even after the component unmounts

119
JavaScript: Sorting array accorting to “outside” parameter

JavaScript: Sorting array accorting to “outside” parameter

In JavaScript, I have an array of objects and an input value like this:

181
Canvas seems to work but doesn't display when I draw on it

Canvas seems to work but doesn't display when I draw on it

I put it online here: http://agency3wcom/carteLocationVelos/

102