如何使用axios请求上传base64

问题描述 投票:0回答:1

对于我在堆栈溢出中发现的所有转换为 Base 64 的函数,它都需要使用 Promise。因此,我创建了一个生成新承诺的函数,并等待它的响应,然后返回该响应。我对要转换为 base64 的所有文件使用此函数,但是由于某种原因,当我带着请求到达后端时,它仍然说它是一个 object.promise,所以我假设等待无法正常工作。

                getBase64 : async (file) => {
                let data = await new Promise((resolve, reject) => {
                    const reader = new FileReader();
                    reader.readAsDataURL(file);
                    reader.onload = () => resolve(reader.result);
                    reader.onerror = (error) => reject(error);
                })
                return data
            },
            submitHistoricalPreprint() {
                
                if (this.$refs['AddHistoricalPreprintForm'].validate()) {

                    let form = new FormData()

                    this.historicalPreprintDocs.forEach((round, index) => {

                        form.append(`data[${index}].ppid`, this.selectPPID.PPID)

                        form.append(`data[${index}].round`, round.round)

                        if (round.preprint) {
                            form.append(`data[${index}].preprint.fileName`, round.preprint.name)
                            form.append(`data[${index}].preprint.content`, this.getBase64(round.preprint))
                        } else {
                            form.append(`data[${index}].preprint`, null)
                        }

                        round.supportingDocumentation ? round.supportingDocumentation.forEach((file, index2) => {
                            form.append(`data[${index}].supportingDocumentation[${index2}].fileName`, file.name)
                            form.append(`data[${index}].supportingDocumentation[${index2}].content`, this.getBase64(file))
                        }) : form.append(`data[${index}].supportingDocumentation`, null)

                        if (round.oactQuestions) {
                            form.append(`data[${index}].oactQuestions.fileName`, round.oactQuestions.name)
                            form.append(`data[${index}].oactQuestions.content`, this.getBase64(round.oactQuestions))
                        } else {
                            form.append(`data[${index}].oactQuestions`, null)
                        }

                        round.cmcsResponses ? round.cmcsResponses.forEach((file,index2) => {
                            form.append(`data[${index}].cmcsResponses[${index2}].fileName`, file.name)
                            form.append(`data[${index}].cmcsResponses[${index2}].content`, this.getBase64(file))

                        }) : form.append(`data[${index}].cmcsResponses`, null)

                        if (round.stateResponses) {
                            form.append(`data[${index}].stateResponses.fileName`, round.stateResponses.name)
                            form.append(`data[${index}].stateResponses.content`, this.getBase64(round.stateResponses))
                        } else {
                            form.append(`data[${index}].stateResponses`, null)
                        }

                        if (round.approvalLetter) {
                            form.append(`data[${index}].approvalLetter.fileName`, round.approvalLetter.name)
                            form.append(`data[${index}].approvalLetter.content`, this.getBase64(round.approvalLetter))
                        } else {
                            form.append(`data[${index}].approvalLetter`, null)
                        }
                    })

                    axios({
                        method: 'post',
                        url: window.location.origin + '/Preprint/UploadHistoricalReview',
                        data: form,
                        headers: {
                            'Content-Type': 'multipart/form-data',
                            "RequestVerificationToken": document.forms[0].querySelector('input[name="__RequestVerificationToken"]').value,
                        }
                    }).then(response => {
                        this.historicalPreprintDocs = [{
                            "round": 1,
                            "preprint": null,
                            "supportingDocumentation": [],
                            "oactQuestions": null,
                            "cmcsResponses": [],
                            "stateResponses": null,
                            "approvalLetter": null,
                        }]
                        this.$refs['AddHistoricalPreprintForm'].reset();

                    }).catch(error => {
                        if (error.response.status == 401 || error.response.status == 402 || error.response.status == 403) {
                            window.location.href = error.response.headers.expiredtokenredirecturi
                        }
                        console.log(error.response.data.error);
                    });
                }
            },
javascript promise
1个回答
0
投票

您不需要承诺只是转换为 Base64。

尝试:

str = "The quick brown fox jumps over the lazy dog";
b64 = btoa(unescape(encodeURIComponent(str)));
str = decodeURIComponent(escape(window.atob(b64)));

请参阅其他答案中的演示/示例:

如何在 JavaScript 中将字符串编码为 Base64?

© www.soinside.com 2019 - 2024. All rights reserved.