Skip to content

Instantly share code, notes, and snippets.

@foloinfo
Last active March 31, 2021 13:15
Show Gist options
  • Save foloinfo/0e82f1b7b3065d2807645f5a33b0a58a to your computer and use it in GitHub Desktop.
Save foloinfo/0e82f1b7b3065d2807645f5a33b0a58a to your computer and use it in GitHub Desktop.
Workaround for uploading files from `assets-library` path on iOS with expo
// img
// {
// uri: '"assets-library://asset/asset.JPG?id=SOME_ID&ext=JPG"',
// md5: "some md5"
// }
// copy into cache dir first
const path = FileSystem.cacheDirectory + img.md5
await FileSystem.copyAsync({
from: img.uri,
to: path
})
// read that and do upload
const file = await FileSystem.readAsStringAsync(path, {
encoding: FileSystem.EncodingTypes.Base64
})
const payload = {
title: 'some awesome photo',
image: file
}
fetch('somewhere', {
method: 'POST',
body: JSON.stringify(payload)
})
@natemartins
Copy link

Can this work without the md5? The package I'm using to get the images is not returning an md5.

@foloinfo
Copy link
Author

@natemartins
You can use any string like random string or generated uuid.

@foloinfo
Copy link
Author

Keep in mind that this gist is from 2019.
It might no work or there maybe better way to handle with the latest version of expo.

@natemartins
Copy link

natemartins commented Feb 2, 2021

Alright. I'll bear that in mind. I need this for multiple image selection and upload which expo does not provide

@DonsWayo
Copy link

You can get the local file system with the method:

const info = await MediaLibrary.getAssetInfoAsync(img)

this returns:

{
  "creationTime": 1617141592000,
  "duration": 0,
  "exif": Object {
    "ColorModel": "RGB",
    "Depth": 8,
    "PixelHeight": 1536,
    "PixelWidth": 2048,
    "{JFIF}": Object {
      "DensityUnit": 0,
      "IsProgressive": true,
      "JFIFVersion": Array [
        1,
        0,
        1,
      ],
      "XDensity": 1,
      "YDensity": 1,
    },
  },
  "filename": "IMG_0070.JPG",
  "height": 1536,
  "id": "146AE19A-6D7B-4274-BCD3-CAA96DA2373D/L0/001",
  "isFavorite": false,
  "isHidden": false,
  "localUri": "file:///var/mobile/Media/DCIM/100APPLE/IMG_0070.JPG",
  "location": null,
  "mediaSubtypes": Array [],
  "mediaType": "photo",
  "modificationTime": 1617152030064,
  "orientation": 1,
  "uri": "assets-library://asset/asset.JPG?id=146AE19A-6D7B-4274-BCD3-CAA96DA2373D&ext=JPG",
  "width": 2048,
}

So the complete example is:

const info = await MediaLibrary.getAssetInfoAsync(img)
	
const fileBase64 = await FileSystem.readAsStringAsync(info.localUri, {
	 encoding: FileSystem.EncodingType.Base64,
})

Hope its help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment