[FileTransfer] Fixed file upload for server 26/249526/2
authorPiotr Kosko <p.kosko@samsung.com>
Fri, 11 Dec 2020 13:21:18 +0000 (14:21 +0100)
committerPiotr Kosko <p.kosko@samsung.com>
Mon, 14 Dec 2020 11:23:16 +0000 (12:23 +0100)
[Issue] File was not properly sent to the server
On 6.0 implementation, problem was partially fixed, but new issues was
introduced - file.size was unknown when success callback was triggered.

[Verification] Described in JIRA task:
https://code.sec.samsung.net/jira/browse/XWALK-2197
TCT passrate 100%

Change-Id: Ic3716c1e2eaa9bc799120adb86aa71c4146064a1

src/lib/plugins/cordova-plugin-file-transfer/tizen/FileTransfer.js

index bcf20cf..f4342a4 100755 (executable)
@@ -110,22 +110,6 @@ exports = {
           }
         }
 
-        xhr.onload = function(evt) {
-
-          if (xhr.status === 200) {
-            uploads[id] && delete uploads[id];
-            successCallback({
-              bytesSent: file.size,
-              responseCode: xhr.status,
-              response: xhr.response
-            });
-          } else if (xhr.status === 404) {
-            fail(FileTransferError.INVALID_URL_ERR, this.status, this.response);
-          } else {
-            fail(FileTransferError.CONNECTION_ERR, this.status, this.response);
-          }
-        };
-
         xhr.ontimeout = function(evt) {
           fail(FileTransferError.CONNECTION_ERR, this.status, this.response);
         };
@@ -164,6 +148,22 @@ exports = {
           }
 
           // sending already initialized request
+          // 'onload' needs to be defined here because it needs blobFile.size
+          xhr.onload = function(evt) {
+            // 2xx codes are valid
+            if (xhr.status >= 200 && xhr.status < 300) {
+              uploads[id] && delete uploads[id];
+              successCallback({
+                bytesSent: blobFile.size,
+                responseCode: xhr.status,
+                response: xhr.response
+              });
+            } else if (xhr.status === 404) {
+              fail(FileTransferError.INVALID_URL_ERR, this.status, this.response);
+            } else {
+              fail(FileTransferError.CONNECTION_ERR, this.status, this.response);
+            }
+          };
           xhr.send(fd);
 
           // Special case when transfer already aborted, but XHR isn't sent.