【问题标题】:React native , image picker , fetch , form data how to post request an imageReact Native,图像选择器,获取,表单数据如何发布请求图像
【发布时间】:2021-12-03 05:56:34
【问题描述】:

我正在尝试将图像发送到我的后端,但我将其作为字符串 [Object Object] 接收

  const data = new FormData();
          //file.uri returns "file:///...." on android and base64 on web 
          data.append('pic', { filename : 'dd.jpeg' , uri : file.uri, type : 'image/jpeg'});
          let res = await fetch(
            'http://192.168.0.88:8000/api/review/new',
            {
              method: 'post',
              body: data,
            }
          );

          let responseJson = await res.json();

在我的 php 后端

$request->file('pic') // returns null
$request->pic // returns [Object Object]

标题(请求)

------WebKitFormBoundaryAmomk6ZVYcf1OVO0
Content-Disposition: form-data; name="pic"

[object Object]
------WebKitFormBoundaryAmomk6ZVYcf1OVO0--

【问题讨论】:

  • 您是否设置了任何标题?
  • 应该是 filename : 'dd.jpeg' 而不是 name : 'dd.jpeg'
  • 我没有设置任何标题。通过这样做解决了它:uri:Platform.OS ===“android”? file.uri : file.uri.replace("file://", "")

标签: laravel react-native fetch fetch-api


【解决方案1】:

这样做解决了:

data.append('pic', { filename : 'dd.jpeg' , uri : Platform.OS === "android" ? file.uri : file.uri.replace("file://", ""), type : 'image/jpeg'});

【讨论】:

    猜你喜欢
    • 2021-05-14
    • 2023-01-31
    • 1970-01-01
    • 2019-02-13
    • 2017-04-12
    • 2021-05-03
    • 1970-01-01
    • 2018-04-07
    • 2020-04-23
    相关资源
    最近更新 更多