Wie lade ich ein Medienfeld vom Typ „Sammlung“ von nextjs14 auf strapiv4 hoch?JavaScript

Javascript-Forum
Guest
 Wie lade ich ein Medienfeld vom Typ „Sammlung“ von nextjs14 auf strapiv4 hoch?

Post by Guest »

Ich versuche, eine Seite zum Hochladen von Dateien in einen „Inhaltstyp“ namens „inscricoes“ zu erstellen, aber bisher hatte ich keinen Erfolg.
Hier ist meine Code: https://pastebin.com/SELMPuRz
In diesem Abschnitt wurden die letzten drei formData.append-Aufrufe später eingeführt, aber sie spielen hier keine wirkliche Rolle case:

Code: Select all

const formData = new FormData();
Array.from(files).forEach((file) => {
formData.append("files", file); // Adding each file
});

formData.append("ref", "api::inscricao.inscricao"); // The content type where the file will be stored
formData.append("refId", inscricaoId); // ID of the "inscricao" to be updated
formData.append("field", "fileLink"); // The field where the file will be saved
Der Datei-Upload funktioniert gut:

Code: Select all

const uploadRes = await fetch("https://api.pnp.cv/api/upload", {
method: "POST",
body: formData,
});
Ich habe jedoch versucht, den Medientyp manuell hinzuzufügen, und nur titulo: file.name und publico: true funktionieren. Die ficheiro.data kommt immer als null zurück:

Code: Select all

const existingFiles = inscricaoData.data.attributes.fileLink || [];

const fileIds = [
...existingFiles, // Keep the existing files
...uploadData.map((file) => ({
titulo: file.name, // File name
publico: true,
ficheiro: {
data: {
attributes: {
id: file.id, // File ID
name: file.name,
url: file.url, // Ensure that the `url` field is returned
mime: file.mime, // MIME type of the file
size: file.size, // File size
},
},
},
})),
];
Dies ist das Ergebnis nach einem Upload:

Code: Select all

{
"data": {
"id": 2,
"attributes": {
"NIF": "456564",
"url": "626179ff-3bba-4e98-8469-58ef4a8e14ae",
"code": "pnp-i",
"nome_completo": "Ailtom Duarte",
"email": "adidas.coyas@gmail.com",
"sede": "Latada",
"telefone": "2389536269",
"categoria": "Branding",
"nome_projeto": "TerraSystem",
"con_criativo": "asasdsfasfasfasfs adfdsfd sfds fdfs ds fsd dsf dfs",
"coord_prod": "yjygjyg",
"dir_foto": "sdfdsf",
"dir_art": "kuhuuluhhu",
"realizador": "sdfsdf",
"autor_jingle": "sdfsdf",
"designer": "fdsfdsfds",
"outras_consideracoes": "fdsfdsfdsf",
"data_producao": "2024-12-24",
"data_divulgacao": "2024-12-16",
"data_apresentacao_publica": "2024-12-25",
"editor": "sdfsd",
"createdAt": "2024-05-01T11:59:56.499Z",
"updatedAt": "2025-01-05T02:04:52.681Z",
"publishedAt": "2024-11-06T12:49:14.559Z",
"fileLink": [
{
"id": 29,
"titulo": "20241031_114101-removebg-preview.png",
"publico": true,
"ficheiro": {
"data": null
}
}
]
}
},
"meta": {}
}
Was ich wirklich tun möchte, ist, mehrere Dateien in das Feld „fileLink“ hochzuladen, wobei ficheiro das Medienfeld ist.

Code: Select all

"fileLink": [
{
"id": 29,
"titulo": "20241031_114101-removebg-preview.png",
"publico": true,
"ficheiro": {
"data": null
}
}
]
Wie setze ich das um?
Wenn es eine bessere Lösung gibt, lassen Sie es mich bitte wissen.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post