Upload Files
First you need to use createFiles
query to get the file url,
and then you have to upload your file to that url.
- GraphQL
- JS Client
mutation {
createFiles(input: [{contentType: "application/zip", extension: "zip"}]) {
fields
mediaDownloadUrl
mediaId
mediaUrl
signedUrl
}
}
client.media.createFiles([{
contentType: "application/zip",
extension: "zip"
}])
To upload the files you need to put all the fields in the fields
in addition to content type and file
to the body of the post request. Here is a JavaScript example of how to do it.
const formData = new FormData()
const parsedFields = JSON.parse(signedUrl.fields)
// The order of appended key-value into the formData matters.
Object.entries(parsedFields).forEach(([key, value]) => {
formData.append(key, String(value))
})
formData.append('Content-Type', file.type)
formData.append('file', file)
fetch(signedUrl.signedUrl, {
method: 'POST',
body: formData
})
Our javascript client have a function to automatically create and upload the files for you.
It takes an extra File
object as an argument.
client.media.uploadFiles([{
contentType: "application/zip",
extension: "zip",
file: file
}])