本文介绍了multer,做出反应,在iOS上无法运行本机图像选择器图像上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在通过提取和react-native-image-picker来上传图像以支持multer和表达js后端时遇到了很大的麻烦.

I am having great trouble in uploading an image via fetch and react-native-image-picker to multer and express js backend.

下面是我的本机代码.

try {

 const data = new FormData();

  data.append('image', {
    name: photo.fileName,
    type: photo.type,
    uri:
      Platform.OS === 'android' ? photo.uri : photo.uri.replace('file://', ''),
  });

 data.append('id', id)

    fetch(`${API}save-image`, {
      method: 'POST',

      body: data,
    })
      .then(response => response.json())
      .then(response => {
        console.log('upload succes', response);
      })
      .catch(error => {
        console.log('upload error', error);
      });
  } catch (error) {
    console.log(error);
    if (error.response !== undefined) {
      message = error.response.data.message;
    } else {
      message = error;
    }

    return Promise.reject(message);
  }

image 变量是我们从react-native-image-picker库中获得的响应对象,该库包含图像数据和uri以及其他所需项目.

image variable is the response object we get from a react-native-image-picker library that contains image data and uri with other needed items.

在后端,我试图登录由multer配置的req.files对象.

in the backend i am trying to log in the req.files object that is configured by multer.

const Storage = multer.diskStorage({
    destination(req, file, callback) {
        callback(null, path.join(__dirname, '../uploads/'));
    },
    filename(req, file, callback) {
        callback(null, new Date().toISOString() + '_' + file.originalname);
    },
});

const upload = multer({
    storage: Storage,
    limits: { fieldSize: 25 * 1024 * 1024 },
});
router.post(
    '/save-image',
    upload.array('image', 3),
    controller.saveImage
);

在控制器中,我将只是 console.log(req.files),然后返回成功消息.

in the controller i will just console.log(req.files) and then return success message.

在android模拟器的情况下,我能够看到控制台日志,但在iOS模拟器上却看不到.

I am able to see the console log in case of android emulator but not on iOS emulator.

实际上,似乎没有将其发送到后端的图像.但是后端没有错误,在iOS中也不会记录该错误.

In fact the image it seems is not being send to the backend. But there is no error on the backend side and it is not logged in case of iOS.

这真令人沮丧

任何人都可以帮忙吗?

推荐答案

0.39 导致上传文件的网络请求失败,请尝试手动更新您的鳍状肢版本.在您的 podfile 中:

versions['Flipper'] ||= '~> 0.51.0'

这篇关于multer,做出反应,在iOS上无法运行本机图像选择器图像上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:03