const encode = (uri) => {
if (Platform.OS === 'android') return encodeURI(`file://${uri}`)
else return uri
}
const _pickDocument = async() => {
try {
let result = await DocumentPicker.getDocumentAsync({copyToCacheDirectory:true, type:'application/pdf'})
console.log(result);
if (result.type !== 'cancel') {
let nameParts = result.name.split('.');
let fileType = nameParts[nameParts.length - 1];
var fileToUpload =
{
name: result.name,
size: result.size,
type: mime.getType(fileType),
uri: encode(result.uri),
}
console.log(fileToUpload);
setDoc(fileToUpload);
}
} catch (error) {
console.log(error);
}
}
const docFormData = new FormData()
docFormData.append('file',doc);
docFormData.append('ACTION', kategoriValue);
docFormData.append('OUTLET', keyCustCode);
docFormData.append('NIK', JSON.parse(keyUserNIK));
const submitDoc= async()=>{
const url = "your php url";
if(kategoriValue==null){
alert("bagian * wajib diisi");
return;
}
setIsLoading(true)
axios.post(url, docFormData,{
headers:{
Accept: 'application/json',
'Content-Type':'multipart/form-data'
}
}).then(response=>{
console.log(response)
setIsLoading(false)
alert(response.data)
}).catch(error=>{
console.log(error)
})
}
this is my code for pick a document and it already gave an uri to work for
Object {
"name": "PA824_User_Manual.pdf", "size": 2659219,
"type": "application/pdf", "uri": "file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540egbertgan%252Fmodern-trade/DocumentPicker/e04a8b31-91b1-4b22-92c4-276bdd219a1f.pdf", }
the size in the example above is 2.6 MB and even though it is succeed but the pdf is somehow empty unless the size is less than 1.8MB.
and below is the response
"responseURL": "your php url", "data": "Ada Kesalahan, file PDF corrupt", "status": 200, "timeout": 0, "upload": XMLHttpRequestEventTarget {}, "withCredentials": true,
And this is the code in server side using PHP
<?php
header("Content-type: application/json; charset=utf-8");
header("Content-type: multipart/form-data");
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
header("Access-Control-Allow-Headers: Content-Disposition, Content-Type, Content-Length, Accept-Encoding");
include "../koneksi.php";
$pesan = '';
$file = $_FILES['file'];
$NIK = $_POST['NIK'];
$OUTLET = $_POST['OUTLET'];
$KATEGORI = $_POST['ACTION'];
$DATE = date('Ymd');
$NAME_FILE = $NIK.'_'.$OUTLET.'_'.$KATEGORI.'_'.$DATE.'.pdf';
$KEY_IN = date('Y-m-d H:i:s');
$folderUpload = 'your folder';
$target_file =$folderUpload . $NAME_FILE;
$file_type = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
$sqlinsert = "insert query";
if ($mysqli->query($sqlinsert) === TRUE) {
$pesan='Data Berhasil Di Upload';
} else {
$pesan = "Error: " . $sqlinsert . "<br>" . $mysqli->error;
}
}
else
{
$pesan='Ada Kesalahan, file PDF corrupt';
}
echo json_encode($pesan);
?>
If someone knows something about this behaviour and can give me a hint I will be grateful.