Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   public sendFile(file: File, visibility: 'public' | 'private' = 'private'): Promise<SocrateFile> {
  2.     const formData = new FormData();
  3.     const xhr = new XMLHttpRequest();
  4.     const url = `${environment.API_URL}files?visibility=${visibility}`;
  5.  
  6.     formData.append('file', file, file.name);
  7.     return this.auth.getJWT().then(jwt => {
  8.       xhr.open('POST', url, true);
  9.       xhr.setRequestHeader('Authorization', jwt);
  10.       return new Promise<SocrateFile>((res, rej) => {
  11.         xhr.onload = () => {
  12.           const response = JSON.parse(xhr.response);
  13.  
  14.           if (!response.id) {
  15.             console.error(response);
  16.             rej();
  17.           } else {
  18.             res(response);
  19.           }
  20.         };
  21.         xhr.send(formData);  // multipart/form-data
  22.       });
  23.     });
  24.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement