Rami Ismail
Rami Ismail

Reputation: 13

react-native Expo FileSystem.downloadAsync respond 400 bad request on ios but on android got 200

my problem is i had 400 bad request when i try to download a file using expo filesystem.downloadAsync on ios devise.

const shareDocument = async (event) => {
const data = JSON.parse(event.nativeEvent.data);
const documentName = data.documentName.replace(/[^a-z0-9.]/gi,'_');
const file = await FileSystem.downloadAsync(
    config.API_DOC_URI + "/documents/pdf/download/" + data.documentId,
    FileSystem.cacheDirectory + documentName,
    {
        headers : {
          'Content-Type': 'application/vnd.api+json',
          'Access-Control-Allow-Origin': '*',
          'Authorization': "Bearer " + data.jwt,
          'X-CSRF-TOKEN': data.csrf,
        }
    }
)
// sharing interface
try {
    if (await Sharing.isAvailableAsync()) {
        await Sharing.shareAsync(file.uri, {UTI:"com.adobe.pdf", mimeType:"application/pdf"});
    }
} catch (e) {
    console.log(e);
}

anyone has an idea how can i fix this issue.

random behavior: NOT ALL IOS DEVISES HAVE THIS PROBLEM.

Upvotes: 1

Views: 1060

Answers (1)

yumol2137
yumol2137

Reputation: 49

You can use await FileSystem.writeAsStringAsync(fileUri, report.data, { encoding: FileSystem.EncodingType.Base64,}); where fileUri is destination url (device specific) and report.data is files url. I don't know in what format your pdf is, mine was buffer array, I ended up converting it to base64, so report.data was base64 string

Upvotes: 1

Related Questions