无法在Laravel项目中上传图像

时间:2019-06-08 14:12:06

标签: php laravel react-native file-upload

我正在尝试使用laravel作为后端框架从react-native项目上传图像。enter image description here->这是我通过抓取发送的内容。enter image description here->这是该错误我明白了 这是我的Laravel代码:

public function upload(Request $request)
  {

    $image = $request->get('data');
    $name = 'Sup.bitch';
    Image::make($request->get('data'))->save(public_path('images/').$name);




    $fileupload = new Fileupload();
    $fileupload->filename=$name;
    $fileupload->save();

    return response()->json(['message' => 'Success']);

 }

这是我的前端代码:

myfun = () => {
    ImagePicker.showImagePicker(options, response => {
      console.log("Response = ", response);

      if (response.didCancel) {
        console.log("User cancelled image picker");
      } else if (response.error) {
        console.log("Image Picker Error: ", response.error);
      } else {
        let source = { uri: response.uri };

        this.setState({
          avatarSource: source,
          pic: response.data
        });
      }
    });
  };



uploadPic = () => {
    console.log(this.state.pic);
    alert("ddf");
    fetch(
      "POST",
      "http://192.168.2.45/backend/public/api/upload",
      {
        Authorization: "Bearer access-token",
        otherHeader: "foo",
        "Content-Type": "multipart/form-data"
      },
      [
        // element with property `filename` will be transformed into `file` in form data
        { name: "image", filename: "avatar.png", data: this.state.pic }
      ]
    ).then(resp => {
      console.log(resp.message);
      alert("your image uploaded successfully");
      this.setState({ avatarSource: null });
    });
  };

0 个答案:

没有答案