garrensmith commented on a change in pull request #1065: Remove JQuery uses 
from documents addon
URL: https://github.com/apache/couchdb-fauxton/pull/1065#discussion_r174039749
 
 

 ##########
 File path: app/addons/documents/doc-editor/actions.js
 ##########
 @@ -156,75 +149,88 @@ function uploadAttachment (params) {
     });
     return;
   }
-
   FauxtonAPI.dispatch({ type: ActionTypes.START_FILE_UPLOAD });
 
-  // store the xhr in parent scope to allow us to cancel any uploads if the 
user closes the modal
-  xhr = $.ajaxSettings.xhr();
-
-  var query = '?rev=' + params.rev;
-  var db = params.doc.getDatabase().safeID();
-  var docId = params.doc.safeID();
-  var file = params.files[0];
-
-  $.ajax({
-    url: FauxtonAPI.urls('document', 'attachment', db, docId, 
encodeURIComponent(file.name), query),
-    type: 'PUT',
-    data: file,
-    contentType: file.type,
-    headers: {
-      Accept: "application/json; charset=utf-8"
-    },
-    processData: false,
-    xhrFields: {
-      withCredentials: true
-    },
-    xhr: function () {
-      xhr.upload.onprogress = function (evt) {
-        var percentComplete = evt.loaded / evt.total * 100;
-        FauxtonAPI.dispatch({
-          type: ActionTypes.SET_FILE_UPLOAD_PERCENTAGE,
-          options: {
-            percent: percentComplete
-          }
-        });
-      };
-      return xhr;
-    },
-    success: function () {
-
-      // re-initialize the document editor. Only announce it's been updated 
when
-      initDocEditor({
-        doc: params.doc,
-        onLoaded: function () {
-          FauxtonAPI.dispatch({ type: ActionTypes.FILE_UPLOAD_SUCCESS });
-          FauxtonAPI.addNotification({
-            msg: 'Document saved successfully.',
-            type: 'success',
-            clear: true
-          });
-        }.bind(this)
-      });
+  const query = '?rev=' + params.rev;
+  const db = params.doc.getDatabase().safeID();
+  const docId = params.doc.safeID();
+  const file = params.files[0];
+  const url = FauxtonAPI.urls('document', 'attachment', db, docId, 
encodeURIComponent(file.name), query);
 
-    },
-    error: function (resp) {
-      // cancelled uploads throw an ajax error but they don't contain a 
response. We don't want to publish an error
-      // event in those cases
-      if (_.isEmpty(resp.responseText)) {
-        return;
-      }
+  const onProgress = (evt) => {
+    if (evt.lengthComputable) {
+      const percentComplete = evt.loaded / evt.total * 100;
       FauxtonAPI.dispatch({
-        type: ActionTypes.FILE_UPLOAD_ERROR,
+        type: ActionTypes.SET_FILE_UPLOAD_PERCENTAGE,
         options: {
-          error: resp.responseJSON ? resp.responseJSON.reason : 'Error 
uploading file: (' + resp.statusText + ')'
+          percent: percentComplete
         }
       });
     }
-  });
+  };
+  const onSuccess = (doc) => {
+    // re-initialize the document editor. Only announce it's been updated when
+    initDocEditor({
+      doc: doc,
+      onLoaded: () => {
+        FauxtonAPI.dispatch({ type: ActionTypes.FILE_UPLOAD_SUCCESS });
+        FauxtonAPI.addNotification({
+          msg: 'Document saved successfully.',
+          type: 'success',
+          clear: true
+        });
+      }
+    });
+  };
+  const onError = (msg) => {
+    FauxtonAPI.dispatch({
+      type: ActionTypes.FILE_UPLOAD_ERROR,
+      options: {
+        error: msg
+      }
+    });
+  };
+  const httpRequest = new XMLHttpRequest();
+  currentUploadHttpRequest = httpRequest;
+  httpRequest.withCredentials = true;
+  if (httpRequest.upload) {
+    httpRequest.upload.onprogress = onProgress;
+  }
+  httpRequest.onloadend = () => {
+    currentUploadHttpRequest = undefined;
+  };
+  httpRequest.onerror = () => {
+    onError('Error uploading file');
+  };
+  httpRequest.onload = (e) => {
+    if (httpRequest.status >= 200 && httpRequest.status < 300) {
+      onSuccess(params.doc);
+    } else {
+      let errorMsg = 'Error uploading file. ';
+      if (e.responseText) {
+        try {
+          const json = JSON.parse(e.responseText);
+          if (json.error) {
+            errorMsg += 'Reason: ' + (json.reason || json.error);
+          }
+        } catch (err) {
+          //ignore parsing error
+        }
+      }
+      onError(errorMsg);
+    }
+  };
+  httpRequest.open('PUT', url);
+  httpRequest.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
+  httpRequest.setRequestHeader('Content-Type', file.type || 
`application/octet-stream`);
+  httpRequest.setRequestHeader('Accept', 'application/json');
+  httpRequest.send(file);
 
 Review comment:
   What is the advantage of using `XMLHttpRequest` over fetch to send the file?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to