I have been trying to follow the documentation on https://cloud.squidex.io/api/docs#operation/Assets_PostAsset that shows how to upload an asset, the documentation simply states that body should be “string Nullable”, I do not understand what what byte string do you require ?? byte string of the element? only the image byte string? what kind of encoding to use?
The snippet above shows only one of my attempts, I have also tried:
Converting the image itself to a byte array
using only tool to convert the image and pasting the string to the formdata
adding mimetype and filesize
My question is what is the correct way to upload an asset?
I’m submitting a…
[ ] Regression (a behavior that stopped working in a new release)
Variation 1 - converting the result from the input element to a byte array
async UploadImage(file: File) // HTML <input type="file">
{
const headers = new HttpHeaders()
.set('Content-Type','multipart/form-data') // according to the docs
.set('Accept','application/json'); // according to the docs
file.arrayBuffer().then((data:ArrayBuffer) =>
{
var uint8View = new Uint8Array(data);
let formData = new FormData();
formData.append('file',uint8View.reduce((str, byte) => str + byte.toString(2).padStart(8, '0'), ''));
// Middleware will add the auth header
return this.HttpClient.post(`${environment.apiUrl}api/apps/harsnyrtideildin/assets`, formData, { headers }).toPromise();
});
}
Variation 2 - converting a blob to an image and sending that
UploadImageFromBlob(image: Blob)
{
let reader = new FileReader();
reader.addEventListener("load", () =>
{
const headers = new HttpHeaders()
.set('Content-Type','multipart/form-data') // according to the docs
.set('Accept','application/json'); // according to the docs
let formData = new FormData();
formData.append('file',reader.result);
formData.append('mimeType',"image/jpeg");
// Middleware will add the auth header
return this.HttpClient.post(`${environment.apiUrl}api/apps/harsnyrtideildin/assets`, { formdata:formData}, { headers }).toPromise();
}, false);
if (image)
{
reader.readAsDataURL(image);
}
}
async UploadImage(file: File) // HTML
{
const headers = new HttpHeaders()
.set(‘Content-Type’,‘multipart/form-data’) // according to the docs
.set(‘Accept’,‘application/json’); // according to the docs
const formData = new FormData();
formData.append('file', file);
await this.HttpClient.post(`${environment.apiUrl}api/apps/harsnyrtideildin/assets`, formData, { headers });
}