【问题标题】:How to upload image from React-Native to Laravel backend?如何将图像从 React-Native 上传到 Laravel 后端?
【发布时间】:2021-07-30 19:54:31
【问题描述】:

我制作了一个 Laravel 后端来接收来自 React-Native 应用程序的图像。 我在邮递员上进行了测试,它运行良好。 但是我不确定如何使用邮递员代码编写RN代码。

如何在 React Native 中上传图片? 我使用了“react-native-image-crop-picker”。

【问题讨论】:

    标签: laravel react-native file-upload


    【解决方案1】:

    我找到了答案。

    try{
        let uploadData = new FormData();
        let myHeaders = new Headers();
        myHeaders.append("Content-Type", "multipart/form-data");
        myHeaders.append("gb-auth-token", myToken);
    
        uploadData.append('profile_img', {
            type: 'image/jpeg', 
            uri: profileImage,     // e.g: file:///storage/......./blabla.jpg
            name: 'upload.jpg'
        })
        uploadData.append("customer_id", id);
        const response = await fetch(CUSTOMER_PP, {
            method: 'POST',
            headers: myHeaders,
            body: uploadData
        })
        const data = await response.json();
        return data;
    } catch (err) {
        console.log(err);
    }
    

    在这段代码中,这是核心。

        uploadData.append('profile_img', {
            type: 'image/jpeg', 
            uri: profileImage,     // This value comes from "react-native-image-crop-picker" response.path
            name: 'upload.jpg'
        })
    

    【讨论】:

    • 谢谢你。效果很好!
    • 很高兴我的回答对您有所帮助。
    猜你喜欢
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    • 2018-01-14
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 2022-10-18
    相关资源
    最近更新 更多