【问题标题】:File is empty in Symfony after uploading with react-native-fetch-blob使用 react-native-fetch-blob 上传后,Symfony 中的文件为空
【发布时间】:2019-09-27 18:02:57
【问题描述】:

我想从 react-native 上传一张图片到 PHP Symfony 服务器。我用 ImagePicker.showImagePicker 选择图片并用 RNFetchBlob.fetch 发送它,但在 Symfony 中该文件似乎是空的。 $file->getMimeType() 返回“文件不存在或不可读”并且文件的内容类型是八位字节流。应该是图片/jpeg。

有什么想法吗? 感谢您的帮助:)

PHP 代码:

private function uploadFile(Request $request, $actualFilename = null)
{
        $file = $request->files->get('userfile');
        var_dump($file->getMimeType());
}

反应原生代码:

const options = {
      title: 'Select Photo',
      takePhotoButtonTitle: "Take photo title",
      chooseFromLibraryButtonTitle: "Choose a photo",
      quality: 1
    };

    ImagePicker.showImagePicker(options, response => {
        deviceStorage.getItem('jwt').then(jwt => {

          const endpoint = 'someEndpoint'

          RNFetchBlob.fetch('POST', endpoint, {
            Authorization : "Bearer " + jwt,
            otherHeader : "foo",
            'Content-Type' : 'multipart/form-data',
          }, [
            { name : 'userfile', filename : 'image.jpg', type:'image/jpeg', data: response.data}
          ]).then((resp) => {
            console.log(resp);
          }).catch((err) => {
            console.log(err);
          });
        });
    });

【问题讨论】:

    标签: symfony react-native react-native-fetch-blob react-native-image-picker


    【解决方案1】:

    你已经传递了图像数据,所以在服务器中你会得到一个 base64 来检索图像,例如:

    private function uploadFile(Request $request, $actualFilename = null){
        $base64Image = $request->get('userfile');
        $data = base64_decode($base64Image);
        file_put_contents($imagePath, $data);
    }
    

    $imagePath 是您保存图像的位置。

    如果您使用 vichuploader 管理图像,请查看此处base64-image-to-image-file-with-symfony-and-vichuploader

    【讨论】:

      猜你喜欢
      • 2018-11-24
      • 2019-12-03
      • 1970-01-01
      • 2017-11-22
      • 2019-02-12
      • 1970-01-01
      • 2018-12-25
      • 1970-01-01
      • 2021-05-20
      相关资源
      最近更新 更多