【问题标题】:how to send image to server in react expo如何在反应博览会中将图像发送到服务器
【发布时间】:2020-05-30 07:24:08
【问题描述】:

我在android studio中通过位图将图像发送到服务器:

  bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), resultUri);

       ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap = Bitmap.createScaledBitmap(bitmap , 500, 500, true);
        bitmap .compress(Bitmap.CompressFormat.JPEG, 100, baos);

        byte[] imageBytes = baos.toByteArray();
        String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);


现在在世博会上我有 uri 使用图像选择器,我该如何在反应世博中做到这一点?

【问题讨论】:

    标签: react-native bitmap expo imagepicker


    【解决方案1】:

    如果后端使用这样的 RestAPI,您可以发送一个 formData

     // Upload the image using the fetch and FormData APIs
      let formData = new FormData();
      // Assume "photo" is the name of the form field the server expects
      formData.append('photo', { uri: localUri, name: filename, type });
    
      return await fetch(YOUR_SERVER_URL, {
        method: 'POST',
        body: formData,
        headers: {
          'content-type': 'multipart/form-data',
        },
      });
    

    【讨论】:

    • 感谢您的关注,我该如何使用“Content-Type”:“application/json”,?我需要发送其他数据来注册服务。@pho
    猜你喜欢
    • 1970-01-01
    • 2021-02-16
    • 2018-09-21
    • 1970-01-01
    • 2022-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多