Home > Enterprise >  How to convert file:// url to https or http url in react native expo using there package?
How to convert file:// url to https or http url in react native expo using there package?

Time:01-29

How to convert file:// url to https or http url in react native expo using there package? how can i do that? like in my case photo.uri is a file:// url and i want to convert it into a https or http url how can i? using expo-file-system,

i tried this:

import * as FileSystem from 'expo-file-system';
import { createDownloadResumable } from 'expo-file-system';

const { uri } = createDownloadResumable(photo.uri, FileSystem.documentDirectory);
let httpsUri = uri.replace("file://" "https://");

Also tried: const { uri } = createDownloadResumable(photo.uri, FileSystem.documentDirectory 'photo.jpg');

NOTHING WORK

that uri becomes undefined

  const handleTakePicture = async () => {
    if (faceData.length === 0) {
      alert('No Face')
    }
    else if (cameraRef.current) {
      const photo = await cameraRef.current.takePictureAsync();
      console.log(photo.uri)
      if (!photo.cancelled) {
        const { uri } = createDownloadResumable(photo.uri, FileSystem.documentDirectory);
        let httpsUri = uri.replace("file://", "https://");
        console.log(`this is the url of the fs ${httpsUri}`)
      }
    }
  }

What can be a better solution?

CodePudding user response:

You can use the fetch method provided by the expo-file-system package to convert a file:// URL to a https or http URL in a React Native Expo application.

Here is an example of how you could use the fetch method to convert a file:// URL to a https URL:

import * as FileSystem from 'expo-file-system';

// ...

const fileUri = 'file://path/to/file.jpg';

const response = await FileSystem.fetch(fileUri, { method: 'HEAD' });

const httpsUrl = response.url;

The above code snippet first import the package and then uses the fetch method to send a HEAD request to the file:// URL. The response object returned by the fetch method contains a url property that holds the https URL of the file.

You can also change the HEAD method to GET to download the file and after that you can use the http or https url to access the file.

Please keep in mind that this will only work if the file you are trying to access is hosted on a web server that supports https or http access.

CodePudding user response:

import { Asset } from 'expo-asset';

// fileUri is a string that contains the file:// URL
const fileUri = 'file://path/to/file';

// Get the https:// or http:// URL of the file
const url = await Asset.fromUri(fileUri).downloadAsync();

console.log(url);

You will need to make sure that the file is available in the local file system by using fetch or XMLHttpRequest to download it.

Alternatively, you can use the react-native-fs package to convert file:// URL to https or http URL.

import RNFS from 'react-native-fs';

const fileUri = 'file://path/to/file';

const url = RNFS.DocumentDirectoryPath   '/'   fileUri.substring(fileUri.lastIndexOf('/')   1);
console.log(url);

if you are using the react-native-fs package, you need to install it first by running npm install react-native-fs --save or yarn add react-native-fs in your project directory.

  • Related