Advertisement
krot

conversejs uploadFile fix

Jul 27th, 2020
1,413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.       uploadFile() {
  2.         const xhr = new XMLHttpRequest();
  3.  
  4.         xhr.onreadystatechange = () => {
  5.           if (xhr.readyState === XMLHttpRequest.DONE) {
  6.             _converse.log("Status: " + xhr.status, converse_chat_Strophe.LogLevel.INFO);
  7.  
  8.             if (xhr.status === 200 || xhr.status === 201) {
  9.               this.save({
  10.                 'upload': _converse.SUCCESS,
  11.                 'oob_url': this.get('get'),
  12.                 'message': this.get('get')
  13.               });
  14.             } else {
  15.               xhr.onerror();
  16.             }
  17.           }
  18.         };
  19.  
  20.         xhr.upload.addEventListener("progress", evt => {
  21.           if (evt.lengthComputable) {
  22.             this.set('progress', evt.loaded / evt.total);
  23.           }
  24.         }, false);
  25.  
  26.         xhr.onerror = () => {
  27.           let message;
  28.  
  29.           if (xhr.responseText) {
  30.             message = __('Sorry, could not succesfully upload your file. Your server’s response: "%1$s"', xhr.responseText);
  31.           } else {
  32.             message = __('Sorry, could not succesfully upload your file.');
  33.           }
  34.  
  35.           this.save({
  36.             'type': 'error',
  37.             'upload': _converse.FAILURE,
  38.             'message': message,
  39.             'ephemeral': true
  40.           });
  41.         };
  42.  
  43.         xhr.open('PUT', this.get('put'), true);
  44.         xhr.setRequestHeader("x-amz-acl",'public-read');//<--
  45.         xhr.setRequestHeader("Content-type", this.file.type);
  46.         xhr.send(this.file);
  47.       }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement