React Native:如何在 React Native 中使用 FileReader

3

我是一名开发应用程序和React Native的新手。我正在开发一个电子签名应用程序,并希望通过导入一个带密码的“.pfx”文件来添加新的认证。我使用react-native-document-picker从设备中选择文件并将其存储在useState中,还有另一个输入框用于输入密码。

我的handleChooseFile代码:

const handleChooseFile = async () => {
        //Opening Document Picker for selection of one file
        try {
            const res = await DocumentPicker.pick({
                type: [DocumentPicker.types.allFiles],
            });

            console.log('res = ', res)
            //Setting the state to show single file attributes
            setSelectedFile(res)

        } catch (err) {
            //Handling any exception (If any)
            if (DocumentPicker.isCancel(err)) {
                //If user canceled the document selection
                alert('Canceled from single doc picker');
            } else {
                //For Unknown Error
                alert('Unknown Error: ' + JSON.stringify(err));
                throw err;
            }
        }
    };

我看到了这段ReactJS用于读取文件的代码,并尝试运行它。

我的handleSubmit代码:

const handleSubmit = (e) => {
        e.preventDefault();

        // Do something with the files
        console.log(selectedFile);

        //const reader = new FileReader();

        const reader = new FileReader();
        reader.onload = function (e) {
            const fileContent = e.target.result;

            // decrypt p12 using the password returned by getPassword(), the password should be protected and not hard coded 
            try {
                // get p12 as ASN.1 object
                const p12Asn1 = forge.asn1.fromDer(fileContent);

                const p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, pwd);

                // get bags by type
                const certBags = p12.getBags({ bagType: forge.pki.oids.certBag });
                const pkeyBags = p12.getBags({ bagType: forge.pki.oids.pkcs8ShroudedKeyBag });
                // fetching certBag
                const certBag = certBags[forge.pki.oids.certBag][0];
                // fetching keyBag
                const keybag = pkeyBags[forge.pki.oids.pkcs8ShroudedKeyBag][0];
                // generate pem from private key
                //var privateKeyPem = forge.pki.privateKeyToPem(keybag.key);
                // generate pem from cert
                //var certificate = forge.pki.certificateToPem(certBag.cert);

                if (certBag && keybag) {
                    console.log(certBag.cert);
                    console.log('Success then upload file!');
                    setStatus('Check password PASS then upload file...');
                    uploadFile();
                }

            } catch (error) {
                console.log(error);
                console.log('Fail!');
                setStatus('Fail!');
            };
        };
        reader.readAsBinaryString(selectedFile);
    }

当我运行这个函数时,会报错:'rader.readAsBinryString is not a function'。如何在React Native中使用FileReader。


1
你可以使用这个库:https://www.npmjs.com/package/react-native-fs - Dipan Sharma
谢谢,让我试试。 - Surapat Pongsuwan
它运行了!!非常感谢。 - Surapat Pongsuwan
1个回答

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接