Last active
August 12, 2020 10:51
-
-
Save DominikAngerer/ee60f2c6a7a703045df87ee8a8fe4fb6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
methods: { | |
... | |
storyblokImageUpload(fileblob, filename, success, failure) { | |
this.api.client.post( | |
`/spaces/${this.spaceId}/assets`, | |
{ filename: filename }, | |
{ headers: { Authorization: this.options.oauthToken } }, // uses the option with the name `oauthToken` which needs to be configured in the schema definition of the field you're using the plugin in. | |
).then((response) => { | |
let request = new XMLHttpRequest() | |
request.withCredentials = false | |
request.open('POST', response.data.post_url) | |
request.onreadystatechange = () => { | |
const allowedStatuses = [200, 201, 204]; | |
if (request.readyState === 4) { | |
if (allowedStatuses.includes(request.status)) { | |
// eslint-disable-next-line | |
console.log('Image uploaded: ' + response.data.pretty_url) | |
success({ location: response.data.pretty_url }) | |
} else { | |
failure(new Error(`Error during upload with status: ${request.status}`)); | |
} | |
} | |
} | |
let formData = new FormData() | |
for (let key in response.data.fields) { | |
formData.append(key, response.data.fields[key]) | |
} | |
formData.append('file', fileblob) | |
request.send(formData) | |
}).catch((error) => { | |
failure(error) | |
}) | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment