redux Expo图像拾取器无法在Android中工作,但可以在IOS中工作

ahy6op9u  于 2023-03-12  发布在  Android
关注(0)|答案(1)|浏览(143)

我试图给予我的React本地博览会为基础的应用程序的用户选择上传图像通过他们的画廊或相机拍摄。它是在iOS中工作,但拒绝在Android中工作。我已经阅读了一切,我可以奠定我的手,但我似乎不能让它工作。我正在使用Android工作室测试应用程序和物理设备连接到博览会Android应用程序。
当我调用处理图片上传的API时,它根本没有到达我的服务器,就抛出了错误。

{"error": "TypeError: Network request failed", "status": "FETCH_ERROR"}

我正在使用Redux Query来进行API请求。我也尝试使用Axios,但仍然得到相同的错误。下面是我如何使用formData来处理图像数据到后端:注:imageInfo来自redux,我在其中存储了Expo Image picker发送的数据:数据如下所示:

{"assetId": null, "base64": null, "duration": null, "exif": null, "height": 2499, "rotation": 
null, "type": "image", "uri":"file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540valvetech%252FchickenEat/
ImagePicker/aab1b518-2e22-463a-b85b-9e402ea62bbd.png", "width": 3332}



const handleUploadImage = async () => {

let uri = imageInfo?.uri
let filename = imageInfo?.uri.split("/")[11]
let type = mine.getType(filename)
let formData = new FormData();
formData.append("name", filename);
 formData.append('file', { uri: uri, name: filename, type: type });

  if(formData){
    console.log(formData, "formdata")
    //called the API endpoint here
    //const res = await axios.post(`${BASE_URL}/upload`, formData)
    await uploadImage(formData)
  }
 
 }

这是我的基本URL,API请求将发送到该URL:

const BASE_URL = "http://my-ip-address:5000/api/v1";

后端使用Node.js, Sequelize, MYSQL
就像我说的,每个其他的API请求都能工作,这个图像上传在IOS中工作没有任何问题。我怎么才能让这个在Android中工作呢?

nxowjjhe

nxowjjhe1#

you must use split. No use photo.uri--- use photo only
const createFormData = (noCase,photo, body = {}) => {
const data = new FormData();
const type=photo.split(".").pop();
const name=photo.split("/").pop();
data.append('case',noCase);
data.append('imagen', {
name: name,
type: `image/${type}`,
uri: Platform.OS === 'ios' ? photo.replace('file://', '') : photo,
});

const handleUploadPhoto = () => {
fetch(`${SERVER_URL}/cargar_fotos`, {
  method: 'POST',
  body: createFormData(1,photo),
})
  .then((response) => response.json())
  .then((response) => {
    console.log('response', response);
  })
  .catch((error) => {
    console.log('error', error);
  });
 };

相关问题