Browse Source

Download file browser files directly from server.

Jason Grout 8 years ago
parent
commit
c57284e319
3 changed files with 10 additions and 18 deletions
  1. 2 2
      src/filebrowser/browser.ts
  2. 2 4
      src/filebrowser/listing.ts
  3. 6 12
      src/filebrowser/model.ts

+ 2 - 2
src/filebrowser/browser.ts

@@ -283,8 +283,8 @@ class FileBrowserWidget extends Widget {
   /**
    * Download the currently selected item(s).
    */
-  download(): Promise<void> {
-    return this._listing.download().then(() => { /* no-op */ });
+  download(): void {
+    this._listing.download();
   }
 
   /**

+ 2 - 4
src/filebrowser/listing.ts

@@ -399,12 +399,10 @@ class DirListing extends Widget {
   /**
    * Download the currently selected item(s).
    */
-  download(): Promise<IContents.IModel> {
+  download(): void {
     for (let item of this._getSelectedItems()) {
       if (item.type !== 'directory') {
-        return this._model.download(item.name).catch(error =>
-          utils.showErrorMessage(this, 'Download file', error)
-        );
+        this._model.download(item.path)
       }
     }
   }

+ 6 - 12
src/filebrowser/model.ts

@@ -208,19 +208,13 @@ class FileBrowserModel implements IDisposable {
    * Download a file.
    *
    * @param - path - The path of the file to be downloaded.
-   *
-   * @returns - A promise which resolves to the file contents.
    */
-  download(path: string): Promise<IContents.IModel> {
-    let normalizePath = Private.normalizePath;
-    path = normalizePath(this._model.path, path);
-    return this._manager.contents.get(path, { content: true, type: 'file', format: 'base64' }).then(contents => {
-      let element = document.createElement('a');
-      element.setAttribute('href', `data:${contents.mimetype};base64,${contents.content}`);
-      element.setAttribute('download', contents.name);
-      element.click();
-      return contents;
-    });
+  download(path: string): void {
+    let url = this._manager.contents.getDownloadUrl(path);
+    let element = document.createElement('a');
+    element.setAttribute('href', url);
+    element.setAttribute('download', '');
+    element.click();
   }
 
   /**