如何将文件链接到 Strapi V4 中的条目(集合类型)?

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

我正在使用 Strapi V4 保存表单的所有数据。在我的 Strapi 架构中,有一个文件/图像字段,其他是文本字段。我想链接此图像和其他条目。目前,当我提交表单时,它会创建条目,但文件字段仍为空,但文件已上传到媒体库,它们没有相互链接。请指导我如何才能将文件链接到条目。

这是我的js代码:

const createQuatation = async (formData) => {

fetch(SERVER_URL + '/', {
    method: 'POST',
    body: formData,
})
    .then(response => {
        if (response.ok) {
            console.log(response.json());
            console.log('File uploaded successfully!');
            $('#exampleModalCenter').modal('hide');
            getQuotationList();
        } else {
            console.log('File upload failed!');
            $('#exampleModalCenter').modal('hide');
            getQuotationList();
        }
    })
    .catch(error => {
        console.error('Error:', error);
    });

}

document.getElementById("saveButton").addEventListener("click", function () {
    const formData = new FormData();

const formElements = formElement.elements;

const data = {};

for (let i = 0; i < formElements.length; i++) {
    const currentElement = formElements[i];
    if (!['submit', 'file'].includes(currentElement.type)) {
        data[currentElement.name] = currentElement.value;
    } else if (currentElement.type === 'file') {
        for (let i = 0; i < currentElement.files.length; i++) {
            const file = currentElement.files[i];
            formData.append(`files.${currentElement.name}`, file, file.name,);
            formData.append('ref', 'api::quotation.quotation');
            formData.append('refId', 14);
            formData.append('field', 'file');
        }
    }
}

formData.append('data', JSON.stringify(data));
for (const value of formData.values()) {
    console.log(value);
}

createQuatation(formData);

});

javascript html strapi web-development-server react-fullstack
1个回答
0
投票

refId 这里应该是一个字符串。

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