0

我以这种方式在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,我该如何在反应世博会上做到这一点?

4

1 回答 1

0

如果后端使用这样的 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',
    },
  });
于 2020-05-30T08:57:22.180 回答