Upload asset what is string <binary>?

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)
  • [ ] Bug report
  • [ ] Performance issue
  • [ X] Documentation issue or request

Current behavior

Throws error

Expected behavior

Uploads the asset

Minimal reproduction of the problem

Environment

  • [ ] Self hosted with docker
  • [X] Self hosted with IIS
  • [ ] Self hosted with other version
  • [ ] Cloud version

Version:1.0.0.0

Browser:

  • [X] Chrome (desktop)
  • [ ] Chrome (Android)
  • [ ] Chrome (iOS)
  • [ ] Firefox
  • [ ] Safari (desktop)
  • [ ] Safari (iOS)
  • [ ] IE
  • [ ] Edge

Sorry, the documentation is a little bit confusing here, I hink you can just add the fiel directly to the formdata without the conversion to a string.

I’ve have tried to submit both a Blob and the file to the formdata and i still got the same error

Can you show the code please? But not as screenshot :wink:

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);
    }
 }
1 Like

Can you please use code blocks

```
CODE
```

But what i meant is that

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 });
}

I’ve tried that, it does not seem to work i keep getting back status 400 with a message “Request body has an invalid format.”

THis works for me:

I tried to use fetch instead of Angular’s HttpClient module, it worked.
I guess HttpClient does something to the body in the asset uploading case.

Thank you for your help!!! 11/10 support :slight_smile:

1 Like

I use angular myself; you could have a look to the code:

  • and -
1 Like