Open PacanvaZelenaya opened 4 years ago
+1
@PacanvaZelenaya / @WillVill I'm also facing the same issue on iOS. Any update on this? Have you guys fixed this issue?
@PacanvaZelenaya / @WillVill I'm also facing the same issue on iOS. Any update on this? Have you guys fixed this issue?
Yes, I prepare a request with an image in little different way:
`import ImageResizer from 'react-native-image-resizer';
export const getFormDataWithImage = async (img) => { console.log({ img });
const format = img.mime === 'image/png' ? 'PNG' : 'JPEG'; const qualityInPercents = 50; let compressedImage; try { compressedImage = await ImageResizer.createResizedImage(img.path, img.width, img.height, format, qualityInPercents); } catch { console.log('Cannot resize image'); }
const formData = new FormData(); const imgPath = Platform.OS === 'android' ? compressedImage.uri : compressedImage.path; try { const res = await fetch(imgPath); const blob = await res.blob(); const imageFile = new File([blob], compressedImage.name); formData.append('file', { uri: imgPath, name: compressedImage.name, type: img.mime }); formData.append('size', imageFile.data.size); } catch { console.log('Cannot handle a blob'); } return formData; };`
Dont pay attention to react-native-image-resizer, you can rewrite the code without it.
Thanks @PacanvaZelenaya , I had did a quick workaround of updating the quality level in node_modules. I didn't faced any issue with that change. If i faced any issue, i will try you above code.
Hi @PacanvaZelenaya, this is because you are using the size of the original file, but using the path from the temp image. Use this method the get correct data from the original file:
import RNFetchBlob from 'rn-fetch-blob';
ImagePicker.openPicker({ }).then(file => { const { sourceURL } = file; filePath = sourceURL.replace('file://', ''); const dataFromOriginalFile = await RNFetchBlob.fs.stat(filePath) }); });
+1
This is because you are using the size of the original file, but using the path from the temp image.
@gomesbreno Do you have any evidence/ refer link to make sure your conviction is right?
Version
Platform
Actual behaviour
I have found one strange thing when upload an image to a server from iOS application: value of "size" parameter of an image object (returned by ImagePicker.openPicker function) is not equal to size of this file that comes to server. I have prepared a simple client and API to reproduce described, you can find them here - https://bitbucket.org/YuriiPrime/. ('client' and 'api' branches respectively) API prints out an incoming request like:
{ fields: { size: '44984' } } { files: { file: File { _events: [Object: null prototype] {}, _eventsCount: 0, _maxListeners: undefined, size: 80921, path: '/var/folders/fr/x6r9rgcn7plbs1yxz1b9w33r0000gn/T/upload_72b2acbd754cd83996d750cdc38238b3', name: 'BF4A74F1-E1A6-451C-A2F3-4F063B2170E9.jpg', type: 'image/jpeg', hash: null, lastModifiedDate: 2020-02-13T08:28:14.476Z, _writeStream: [WriteStream] } } }
You can see different sizes - size: '44984' that was given from an image object, and size: 80921 of a binary data. If to upload an image from Android application those parameters has the same value. Why so?