2

文件是图像已上传,但文件大小为 0,无法查看图像。此外,按下上传按钮后,应用程序会冻结并自行关闭。下面是我写的代码。

uploadingPhotos() 
{ 
    RNFetchBlob.fetch('POST','localhost/uploadingImage.php',
        {
            Authorization: "Bearer access-token",
            otherHeader: "foo",
            'Content-Type': 'multipart/form-data',
        },
        [
            {name: 'image', filename: 'image.png', type: 'image/png', data:RNFetchBlob.wrap(this.state.data)},
        ])
        .then((resp) => {
           // ...
        })
        .catch((err) => {
            // ...
        })
}


uploadingImage.php
<?php

$target_dir="uploads";

$target_dir=$target_dir."/".rand().'_'.time().".jpeg";

if(move_uploaded_file($_FILES['image']['tmp_name'],$target_dir)){
  echo json_encode([
    "Message"=>"The file has been uploaded.",
    "Status"=>"OK"
]);

}
else{
echo json_encode([
    "Message"=>"Sorry, there was an error.",
    "Status"=>"Error"
]);
}

?>
4

0 回答 0