I use rest to post upload file (no file_entity, but core file) following direction in https://www.drupal.org/project/drupal/issues/1927648

In the app I use FileRider to get file binary for post body.

  reader.onload = () => {        
    let data = reader.result;
    let file_name = event.target.files[0].name;

    this.rest.postFile(uri, data, file_name).subscribe(res => {
      this.saved_file = res;



post uri

  let uri = `/file/upload/${entity_type_id}/${bundle}/${field_name}?_format=json`;

rest.postFile is

  postFile(uri: string, file: File, filename) {
    return this.http.post(`${this.api_endpoint}${uri}`, file, {
      headers: new HttpHeaders()
        .set('Content-Type', 'application/octet-stream')
        // Content-Disposition:file;filename="example.txt"
        .set('Content-Disposition', `file;filename="${filename}"`)
        .set('api-key', this.api_key)

File is uploaded and saved in proper place set in bundle field. File object is properly set filename, mimetype, status, uri and saved in drupal. The file itself is saved with a proper filename, size, mimetype application/octet-stream which cause system it is corrupted.

  $ file -i file-name.png 
  file-name.png: application/octet-stream; charset=binary

The same is for .docx, .pdf, .txt, .jpg - all files are uploaded, saved and attached to node (and changes staus to true), but they are corrupted - $ file -i show filename.png: application/octet-stream; charset=binary

What also I miss?


xubuntu-16.04 latest

Apache/2.4.34 (Ubuntu)

PHP Version 7.2.9-1+ubuntu16.04.1+deb.sury.org+1 FPM/FastCGI

Drupal-8.5.6 patch applied https://www.drupal.org/project/drupal/issues/1927648?page=1#comment-12685734, patch link https://www.drupal.org/files/issues/2018-07-13/1927648-581.patch


All work as expected when 'data' is file = event.target.files[0] No need of FileRider.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.